2

I'm using a UINavigationController to navigate between classes. When I press a button on the first view to send me to the second one, everythin works fine. But when I wan't to return from the second, to the first view, the viewDidLoad method isn't being called, and I really need that to happen.

According to this post, I should somehow set my view to nil but I'm not sure where or how to do that. This is the code that I'm using to return to the first view:

NewSongController *nsContr = [[NewSongController alloc] initWithNibName:@"mNSController" bundle:nil];
[self.navigationController popViewControllerAnimated:YES];
[nsContr release];
Community
  • 1
  • 1

3 Answers3

5

Your code is not correct.

You don't need to instantiate your first controller in order to pop to it. It already exists.

viewDidLoad will only run when you load the viewController for the first time (i.e. when you push to it). When you push to other controllers they are put onto a stack (imagine a stack of cards). When you push another card onto the stack the cards beneath it are already there.

When you pop it is like removing a card from the stack. But the card underneath is already there so it doesn't need to load again. All it does is run viewWillAppear.

All you need to do to pop back is...

[self.navigationController popViewControllerAnimated:YES];

That's it.

Remove the stuff about NewSongController (if that is what you are trying to go back to).

Then in the NewSongController function - (void)viewWillAppear:animated; put the code you want to run when you get back to it.

Hope that helps.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I didn't know about the `viewWillAppear`method. Thanks for pointing out the unnecessary code. –  Sep 20 '12 at 17:13
1

Your first view is loaded and is pushed onto the navigation stack. Dont try to mess with whats on the stack without fully understanding how setting the view to nil will affect the behavior.

Whatever you do on viewDidLoad, doing it in viewWillAppear or viewDidAppear will give you the result you want.

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
0

The viewDidLoad wouldn't appear because it already exists in the navigation stack. Since you are going back in the stack you would want to have the code that you want to trigger in viewWillAppear or viewDidAppear which is executed when popping from one viewcontroller to the one below it.

propstm
  • 3,461
  • 5
  • 28
  • 41