epologee
epologee

Reputation: 11319

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?

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.

Upvotes: 10

Views: 6815

Answers (2)

Dan Ray
Dan Ray

Reputation: 21893

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.

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72981

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

Upvotes: 9

Related Questions