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 UIViewController
s 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.
I believe I'd go the other direction on this, and try to catch the polling from the individual viewControllers rather than the navigationController. To an individual viewController, getting popped looks like it's being deallocated, and that's totally hookable.
Subclass UIViewController, implement your notification in its
-dealloc
. Be sure to call[super dealloc]
.Then have every view that you push into your navigation controller subclass your new custom view controller subclass. They can do whatever they do in their own viewDidUnload, and then call
[super dealloc]
(in this casesuper
is your UIViewController subclass) to fire the notification.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