Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidLoad (and loadView) is not fired after the view controller is pushed into navigation controller

viewController's view is not loaded just after that viewController is pushed into navigation controller.

This is my code snippet.

- (void)myMethodInClassA {
    // window's root view controller is navigation controller
    UINavigationController *naviCtrl = (UINavigationController*)[UIApplication sharedApplication].keyWindow.rootViewController;
    MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    [naviCtrl pushViewController:myVC animated:NO];

    // at this point, myVC's view is NOT loaded
}

When I call myMethodInClassA, myVC's viewDidLoad is called AFTER that method returns. I'd expected that myVC's view is loaded just after navigation controller's pushViewController:animated: is called and before myMethodInClassA returns.

When exactly view controller's view is loaded? Apple's documentation just says it is loaded when it is first accessed. It's a bit ambiguous. why doesn't navigation controller's pushViewController: access view controller's view?

p.s. sorry for initial ambiguous question.

like image 913
minorblend Avatar asked Dec 12 '22 14:12

minorblend


2 Answers

Pushing a view controller (VC) onto a navigation controller's stack makes the VC into a child view controller of the navigation controller (which is a container view controller). Creating such a child-parent relationship is a distinct step which does not cause the child VC's view to be loaded immediately. Rather the container VC loads the view at a later time. I believe there is no explicit specification for what "later" means - usually it will be when the container VC has decided that the time has come to integrate the child VC's view into the container VC's view hierarchy. But basically it simply happens at the discretion of the container VC's implementation.

That being said, anyone can force a VC's view to be loaded by simply accessing the VC's view property. For instance, in your code you could add this line

myVC.view;

which would trigger loadView and then viewDidLoad in MyViewController.

However, in your case if MyViewController needs to react to the event that it has been associated with a container VC, then it would be better to override one (or both?) of the following methods in MyViewController:

- (void) willMoveToParentViewController:(UIViewController*)parent
{
    // write your code here
}

- (void) didMoveToParentViewController:(UIViewController*)parent
{
    // write your code here
}

You need to be aware, though, that willMoveToParentViewController and didMoveToParentViewController are also invoked when MyViewController is popped from its parent navigation controller's stack. You can detect that this is the case by checking the parent argument for nil.

like image 92
herzbube Avatar answered Dec 26 '22 00:12

herzbube


(Swift 2) Since this question doesn't have an accepted answer...

What I ended up doing is create a convenience init at the child view controller:

convenience init() {
    self.init(nibName: "ChildViewController", bundle: nil)
    //initializing the view Controller form specified NIB file
}

and in the parentViewController's viewDidLoad():

let commentsView = CommentsViewController()
self.addChildViewController(commentsView)
self.momentsScrollView.addSubview(commentsView.view)
commentsView.didMoveToParentViewController(self)
like image 40
Andre Simon Avatar answered Dec 26 '22 00:12

Andre Simon