开发者

View load methods not firing

I'm loading a view like this:

if(commentSubmissionView == nil){
    commentSubmissionView = [[CommentSubmissionController alloc] initWithNibName:@"CommentSubmissionView" bundle:nil];  
}
[self.view addSubview:commentSubmissionView.view];

viewDidLoad and viewWillAppear in commentSubmissionView do not fire. Is 开发者_C百科there something else I need to do?


The documentation for viewWillAppear: isn't completely clear but have a look at what it says:

This method is called in response to a view being added either directly or indirectly to a window. You add views to a window directly by using the addSubview: method to add them to the window’s view hierarchy. You can also add a view indirectly in several ways, including by pushing it onto a navigation stack or by presenting it modally (using the presentModalViewController:animated: method). This method is called before the view is actually added to the window and before any animations are configured.

The viewDidAppear: and viewWillAppear: methods will only be called if you are:

  1. Adding them to a window (adding to a view does not count -- it must be a window)
  2. Using pushViewController: or presentModalViewController:animated: or switching tabs in a tab controller or any other situation where an Apple-provided view controller adds the view controller and its view for you.

In the situation you have, where you are adding a view to another view, you must invoke viewWillAppear: and viewDidAppear: on the view controller yourself or they won't happen.

I don't know why viewDidLoad was not getting invoked for you initially. It will always be invoked after loadView, when the view property accessor is invoked on the controller for the first time, unless an error occurs. It is possible the view load was failing (you'd have to check the console log).

You do not need to follow addSubview: with bringSubviewToFront: since addSubview: always adds to the front. Again, from the documentation of addSubview:

Adds a view to the receiver’s subviews so it’s displayed above its siblings.


If you're using addSubview: then you also need to use bringSubviewToFront: as follows:

if(commentSubmissionView == nil){
    commentSubmissionView = [[CommentSubmissionController alloc] initWithNibName:@"CommentSubmissionView" bundle:nil];  
}
[self.view addSubview:commentSubmissionView.view];
[self.view bringSubviewToFront:commentSubmissionView.view];


viewDidLoad and viewWillAppear are called on UIViewController subclasses, commentSubmissionView looks like a UIView subclass. As Matt mentioned, you probably want to push/present a view controller instead of just adding a subview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜