简体   繁体   English

子类化UIViewController,重复调用viewDidLoad

[英]Subclassing UIViewController, viewDidLoad called repeatedly

I subclassed UIViewController as STViewController and noticed that classes inheriting from STViewController have their viewDidLoad method being called repeatedly. 我将UIViewController子类化为STViewController并注意到从STViewController继承的类会重复调用它们的viewDidLoad方法。 Ultimately crashing the app. 最终崩溃的应用程序。 STViewController is basically a blank implementation at this point. STViewController基本上是一个空白实现。 I am subclassing as shown below: 我是子类,如下所示:

#import "STViewController.h"

@interface WelcomeViewController : STViewController {

STViewController.h STViewController.h

#import <UIKit/UIKit.h>

@interface STViewController : UIViewController
{
}
@end

STViewController.m STViewController.m

#import "STViewController.h"


@implementation STViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

viewDidLoad() from WelcomeViewController.m 来自WelcomeViewController.m的viewDidLoad()

- (void)viewDidLoad
{
    [super viewDidLoad];

    // hide the buttons
    [[self signUp] setHidden: YES];
    [[self logIn] setHidden: YES];
}

You are overriding loadView , but your implementation is empty, and you're not assigning a view. 您正在覆盖loadView ,但您的实现是空的,并且您没有分配视图。 Remove the loadView override. 删除loadView覆盖。

From UIViewController Class Reference (emphasis mine): UIViewController类参考 (强调我的):

You should never call this method directly. 你永远不应该直接调用这个方法。 The view controller calls this method when the view property is requested but is currently nil. 视图控制器在请求视图属性时调用此方法,但当前为nil。 If you create your views manually, you must override this method and use it to create your views. 如果手动创建视图,则必须覆盖此方法并使用它来创建视图。 If you use Interface Builder to create your views and initialize the view controller—that is, you initialize the view using the initWithNibName:bundle: method, set the nibName and nibBundle properties directly, or create both your views and view controller in Interface Builder—then you must not override this method. 如果使用Interface Builder创建视图并初始化视图控制器 - 也就是说,使用initWithNibName:bundle:方法初始化视图,直接设置nibName和nibBundle属性,或者在Interface Builder中创建视图和视图控制器 - 那么你不能覆盖这个方法。

The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. 此方法的默认实现查找有效的nib信息,并使用该信息加载关联的nib文件。 If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view . 如果未指定nib信息, 则默认实现会创建纯UIView对象并使其成为主视图

If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property . 如果为了手动创建视图而重写此方法,则应该这样做并将层次结构的根视图分配给view属性 (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super. (您创建的视图应该是唯一的实例,不应与任何其他视图控制器对象共享。)此方法的自定义实现不应调用super。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 -viewDidLoad未在子类UIViewController中调用 - -viewDidLoad not called in subclassed UIViewController 子类化 UIViewController 时调用 viewDidLoad 是什么? - What invokes viewDidLoad when subclassing UIViewController? 推送UIViewController时未调用viewDidLoad - viewDidLoad not being called when pushing UIViewController 没有使用我的自定义UIViewController调用ViewDidLoad - ViewDidLoad not being called with my custom UIViewController UIViewController和UITabBarController应用-viewDidLoad仅调用一次 - UIViewController and UITabBarController app - viewDidLoad called only once UIViewController viewDidLoad - UIViewController viewDidLoad 当UIViewController中有某些东西时,在调用viewDidLoad时不调用viewWillAppear和viewDidAppear - viewWillAppear and viewDidAppear not called while viewDidLoad called when something is in the UIViewController 推送到新的UIViewController后,强制再次调用viewDidLoad - Forcing viewDidLoad to be called again after pushing to a new UIViewController UIViewController类别不适用于viewDidLoad - UIViewController category not working on viewDidLoad UINavigationController的一种方法,作为UIViewController的“ viewDidLoad” - A method to UINavigationController as “viewDidLoad” to UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM