简体   繁体   English

在initWithNibName之后调用ViewDidLoad

[英]Invocation of ViewDidLoad after initWithNibName

It appears that ViewDidLoad() is sent to a ViewController only after its View is physically displayed (ie via NavigationController pushViewController), and not immediately after initWithNibName() . 看来,仅在物理上显示ViewDidLoad()之后(即通过NavigationController pushViewController)才将ViewDidLoad()发送给ViewController,而不是在initWithNibName()之后立即发送。 Is this a behavior I can rely on? 这是我可以依靠的行为吗? I would like to get the chance to set the member variables of my view so that all the members are valid by the time ViewDidLoad() is invoked. 我希望有机会设置视图的成员变量,以便在调用ViewDidLoad()时所有成员均有效。

You can set up member variables and other such things in initWithNibName:bundle: . 您可以在initWithNibName:bundle:中设置成员变量和其他类似的东西。

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
    if (self = [super initWithNibName:nibName bundle:nibBundle]) {
        // set up ivars and other stuff here.
        someIvar = someValue;
    }
    return self;
}

You are correct that viewDidLoad: is only sent when the view is physically displayed, ie when it is added to some visible view (which may sometimes be never if the user does not reach that view). 您是正确的,只有当视图实际显示时才发送viewDidLoad:,即,将其添加到某个可见视图时发送(有时,如果用户未到达该视图,则可能永远不会发送)。 So it's useful to split the functionality and think about what you can do at init time and what happens at view load time. 因此,拆分功能并考虑在初始化时可以做什么以及在视图加载时会发生什么是很有用的。

As Marcelo Cantos notes in the comment, viewDidLoad: is generally a fine place to do all sorts of setup work, using the concept of "lazy loading," so that you defer the setup until as late time as possible. 正如Marcelo Cantos在评论中指出的那样,viewDidLoad:通常是使用“延迟加载”的概念进行各种设置工作的好地方,因此您可以将设置推迟到尽可能晚的时间。

viewDidLoad is called before a view controller is displayed for the first time, not immediately after initWithNibName . 在第一次显示视图控制器之前(而不是在initWithNibName之后立即)调用viewDidLoad For example, if you have a tab bar controller, all of the child view controllers will be init d at launch, but viewDidLoad will only be called when you click on the appropriate tab the first time. 例如,如果您有一个标签栏控制器,则所有子视图控制器将在启动时被init ,但是只有当您第一次单击适当的选项卡时,才会调用viewDidLoad It's generally a good idea to initialize memory-intensive items in viewDidLoad , so as to avoid using unnecessary memory. 通常,最好在viewDidLoad初始化占用大量内存的项目,以避免使用不必要的内存。

Sorry to unearth an old thread, but this solved it for me... 很抱歉,发现了一个旧线程,但这为我解决了...

-(void)viewDidLoad is only called after -(void)loadView has done its thing. 仅在-(void)loadView完成其操作后才调用-(void)viewDidLoad。 In the docs for loadView: 在用于loadView的文档中:

The view controller calls this method when its view property is requested but is currently nil. 当请求其视图属性但当前为nil时,视图控制器将调用此方法。

My view controller only has viewDidLoad called after its view is request by a UITabBarItem, meaning viewDidLoad is only called in the viewController once the tab bar button is pressed. 我的视图控制器仅在UITabBarItem请求其视图后才调用viewDidLoad,这意味着仅在按下选项卡栏按钮后,才在viewController中调用viewDidLoad。 I, like the OP, want viewDidLoad to be called directly after the nib is loaded, so it's contents (titles, etc) can be populated before the user clicks the tab button. 我像OP一样,希望在装入笔尖后直接调用viewDidLoad,因此可以在用户单击选项卡按钮之前填充其内容(标题等)。

So, after calling "self = [super initWithNibName:@"nibName" bundle:nil];" 因此,在调用“ self = [super initWithNibName:@“ nibName” bundle:nil];之后”; in the view controller's custom initialiser, I immediately called '[self view]' afterwards. 在视图控制器的自定义初始化程序中,此后我立即调用了[[self view]”。 As the view is requested earlier than when it is requested by the UITabBarItem (which calls 'addSubview'), the view is initialised fully during initialisation, rather than when requested. 由于视图是在UITabBarItem(称为“ addSubview”)请求之前提早请求的,因此该视图在初始化期间(而不是在请求时)完全初始化。

Hope this helps. 希望这可以帮助。

I found that if I override initiWithNibName in the view controller, the viewDidLoad method is not called. 我发现如果在视图控制器中重写initiWithNibName,则不会调用viewDidLoad方法。 I have to call it manually [self viewDidLoad]. 我必须手动调用它[self viewDidLoad]。 But if I do not override initWithNibName: viewDidLoad is called. 但是,如果我不重写initWithNibName:将调用viewDidLoad。 I am working with 4 view controllers in tab bar controller. 我正在标签栏控制器中使用4个视图控制器。 the tab bar controller is loaded from another view. 标签栏控制器从另一个视图加载。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM