Search code examples
iosobjective-cuinavigationcontrollerviewdidload

viewDidLoad getting called every time after calling popViewControllerAnimated:


I have a vc called ViewControllerOne, when the user taps a UITableViewCell I call a push segue and navigate to ViewControllerTwo. In ViewControllerTwo I'm hiding the navigation bar, therefore I've created a custom back button:

- (IBAction)backBttn:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

that works perfectly, but the viewDidLoad getting called (in ViewControllerTwo) every time I navigate back to ViewControllerOne and than open ViewControllerTwo again. Am I right that viewDidLoad getting called because I'm using [self.navigationController popViewControllerAnimated:YES]? Or it must have another reason?


Solution

  • If I understand you right you are seeing viewDidLoad called on the view controller you are pushing to. (ViewControllerTwo). If you push from ViewControllerOne to ViewControllerTwo, ViewControllerTwo's viewDidLoad is being called.

    If you then click the back button to pop ViewControllerTwo, return to ViewControllerOne, then push to ViewControllerTwo again, you see viewDidLoad being called a second time.

    This is expected behavior. Push segues (and all other segues except unwind segues) create a new instance of the view controller they are presenting.

    Likewise, popping/dismissing deallocates the view controller you are leaving.