viewDidLoad getting called every time after calling popViewControllerAnimated:

770 views Asked by At

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?

2

There are 2 answers

2
Duncan C On BEST ANSWER

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.

0
ndmeiri On

When you pop the view controller from the navigation stack, the view that it manages is deallocated (or unloaded). Therefore, when you open ViewControllerTwo again, the view must be loaded again. This is why viewDidLoad is called multiple times in your case.