Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get notified when a view controller is about to be popped in iOS4

This question has been asked before, but the answered ones I could find were from 2009 and don't suit my problem. Let me reiterate the issue.

I have a UINavigationController that spawns and pushes lots of different UIViewControllers onto its stack. One of those deals with some Core Data operations that need to be saved when that one particular VC get's popped off the stack. Don't focus on the Core Data part, it's about the popping.

How can I hook into the moment that the UIViewController is going to be popped off the stack?

  • I was hoping for a delegate method of some sort, but couldn't find it. The UINavigationControllerDelegate protocol is very sparse.
  • I then started thinking of using viewWillDisappear, but that one is also called if another view is pushed onto the stack, so it doesn't provide the right moment.
  • This answered question, from 2009, opts to look at the viewWillAppear of the view controller that we're 'popping to', but since that call doesn't have a reference to the VC that needs to do the checking, this is unsatisfactory and will introduce a level of dependency that is counter productive (the VC is used by several NCs).
  • Another answered question, also from 2009, opts to subclass UINavigationController and rewrite the popViewControllerAnimated: method. Or alternatively use the VC's dealloc. My gut tells me that can't be the way to go.
  • Finally there's one last recent question from march 2011, but no one cared to answer it.

That leaves me in my current unsatisfied state of mind. Is there anyone out there with a better solution to finding the moment your UIViewController is popped off a UINavigationController's stack?

Cheers,
EP.

like image 641
epologee Avatar asked May 13 '11 12:05

epologee


1 Answers

viewWillDisappear is the appropriate delegate. You will need to add logic within this method if you want to determine if the current view is being popped or a new view is being pushed. That's been answered here - viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

like image 67
Jason McCreary Avatar answered Oct 24 '22 10:10

Jason McCreary