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

2

10

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.

Intellectual answered 13/5, 2011 at 12:21 Comment(1)
I face the same problem. However, i can add some information according to developer.apple.com/library/ios/#documentation/uikit/reference/… both viewDidUnload & viewWillUnload are deprecated under iOS6Litter
S
9

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

Shirleyshirlie answered 13/5, 2011 at 12:30 Comment(4)
Spot on, wrapped it in a category and works like a charm. Thanks @Jason!Intellectual
I just had a look at your answer. It looks nice but a bit too complex for what is really needed. I suggest that this could be cleaner: if (![self.navigationController.viewControllers containsObject:self]) { //POPPED! }Burbank
Good one. Would be weird to have the same VC in a navigation controller's stack twice anyway. Cheers!Intellectual
@Pier, good point. That answer had some dated code. I was simply relaying the appropriate delegate to use. Thanks for the update.Shirleyshirlie
F
0

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 case super is your UIViewController subclass) to fire the notification.

Flatulent answered 13/5, 2011 at 12:30 Comment(4)
This wouldn't work. If you pop a VC from a navigation Controller stack, it won't go through -viewDidUnloadBurbank
@Pier - You sure? I've done something like this, but it's been a while... Did I catch dealloc maybe?Flatulent
Yes -dealloc is called. viewDidUnload is only called when the VC receives a memory warning and is not the top view controller.Burbank
Actually, dealloc is not neccessarily called if the view controller is persistent somewhere, i.e. not released at the same time that it disappears. Jason's reference to the other question was on point though. Thanks!Intellectual

© 2022 - 2024 — McMap. All rights reserved.