0

I'm relatively new to iOS development and I have an issue with popping view controllers. I need to pop two (or more) UIViewControllers from the navigation stack when back button is pressed. Since I don't want them all to be animated I'm first popping all but last one un-animated and then last one animated, but then -viewWillDisappear is not getting called only for that last one. Here's my code (these are getting called from super UIViewController that all others are extended from):

//popping all but last one - viewWillDisappear getting called for these
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:j] animated:NO];
//popping last one - viewWillDisappear not getting called for this one
[self.navigationController popViewControllerAnimated:YES];
//also tried this but with same result:
//[((UIViewController*)[self.navigationController.viewControllers objectAtIndex:j]).navigationController popViewControllerAnimated:YES];

Does someone know what am I doing wrong here or can you instruct me how to correctly achieve what I need?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
mdmilic
  • 13
  • 1
  • 4

1 Answers1

1

viewWillDisappear has already been called when you load a new controller. Offhand I would imagine only the top controllers viewWillDissappear would be called when doing something like this.

Also why not just pop to the desired view animated?

If you put a nslog in view will appear you'll probably find an equal amount being called for view will disappear.

Let me know what you find. Hope that helps.

Hackmodford
  • 3,901
  • 4
  • 35
  • 78
  • Thanks for your input @Hackmodford. Not sure why I didnt pop to the one I want animated straight away. Now I tried it but with the same results. When I pop, no matter how many UIViewController's from the stack (let's say 3), it will always call viewWillDisappear for the one that was shown (3rd), but then it will call viewWillAppear for the last one it popped (1st one), wont call viewWillDisappear for it, and will call viewWillAppear for the one that is about to be displayed (number 0). Maybe better question is why would it call viewWillAppear for #1 if it's not displaying it. Hope this helps. – mdmilic Sep 05 '12 at 23:30
  • Also probably what i should mentioned is that I used solution from [this SO article](http://stackoverflow.com/questions/1214965/setting-action-for-back-button-in-navigation-controller/3445994#3445994), to detect when back button is pressed so I could override it. This means that all that popping is done in super VC's viewWillDisappear when VC that was displayed calls '[supper viewWillDisappear]' in its own viewWillDisappear. – mdmilic Sep 05 '12 at 23:36
  • Yes I've used that example as well in my code. I'm not sure why view #1 is getting viewWillAppear... maybe it's the way it's animating it? In anycase have you found a work around for what you needed? – Hackmodford Sep 06 '12 at 16:23
  • No unfortunately not yet. I'll try few more things and get back if I get a solution. The big problem for me is that i do some stuff in willAppear that I hopped to reset in willDisappear, so this is breaking my logic. – mdmilic Sep 06 '12 at 22:47
  • I'm gonna check my code to see what happens. I think I have a similar case to yours. – Hackmodford Sep 07 '12 at 13:04
  • I just tried popping straight to the viewController. (3)'s viewWillDisappear is called and (0)'s viewWillAppear is called. Nothing was called for (2) or (1). This is different than what you get right? – Hackmodford Sep 07 '12 at 13:13
  • Essentially I can't duplicate your problem... so unless you want to share some code I can't help. :( – Hackmodford Sep 07 '12 at 13:21
  • Well right now I changed my logic to avoid having to pop multiple VC's. Right now whenever I want to push this type of VC, I will first pop the one that is presented and then push the new one. This workaround seems to work OK for now. Thanks for your effort, I appreciate it. – mdmilic Sep 08 '12 at 02:19