Search code examples
iosobjective-cviewdidloadpresentviewcontroller

iOS presentViewController doesn't invoke viewDidLoad


I'm implementing my own 'back' button. Where onClick, the following code is executed in the ViewController (VC) being dismissed:

  1. Dismiss current VC (VC#1)
  2. Pop current VC (VC#1) off my custom navigationStack
  3. Get the last VC (VC#2) from the navigationStack, and present it using

presentViewController

What happens is the back works visually works - i.e. current VC disappears, previous VC appears. However, the viewDidLoad method is not called. So the screen isn't updated with data updates from viewDidLoad.

 [self dismissCurrentViewController:self completion:^{
    [TWStatus dismiss];
    FHBaseViewController *vcToDisplay = [[FHDataManager sharedInstance] popNavigationStack];
    [vcToDisplay.homeVC presentViewController:vcToDisplay animated:NO completion: ^{ }];
}];

Questions:

  1. I was under the impression that viewDidLoad always gets called when presentViuewController is used??
  2. I 'build' the screen using a method called ONLY from viewDidLoad in VC#2. How is iOS displaying the screen without coming into viewDidLoad?

btw, I'm not using storyboards. Any help is appreciated!


Solution

  • My guess is that viewWillAppear is being called but viewDidLoad is not, at least not when you expect it is. viewDidLoad should be called once, but depending on how you're managing the view controllers, viewDidLoad may not be triggered every time your view appears (which happens after loading).

    The completion handler is called after the viewDidAppear: method is called on the presented view controller. from presentViewController doc

    so put this in your code with a breakpoint on the call to super and verify it is getting called when this transition occurs.

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    

    edit: since you verified that viewWillAppear is getting called, then I would say that it's coming down to how you are managing the view controller life cycle. Even with a standard UINavigationController, viewDidLoad is not called when a view is shown as a result of popping items on the navigation stack. I would move your logic to viewWillAppear if you are dead set on not using UINavigationController