Get Notified of popViewController

2020-07-31 06:27发布

问题:

I need to save my data by calling a method I already have when a viewController is popped using the back button created by the UINavigationController.

Is there a way to get a delegate callback or a notification I didn't see anything in the documentation?

回答1:

You will be notified that the view will be disappearing, with the view controller method viewWillDisappear:, however, this will be called each time the view is moved offscreen, whether that means the controller is popped or a new controller is pushed, or whatever else may cause your view to disappear.

Perhaps a better design would be to save your data in your controllers dealloc method. Normally, a navigation controller is the owner of a view pushed into it's stack, so popping it usually causes it to deallocate. This isn't always the case though and depends on how you've written your app.



回答2:

In your viewWillDisappear method, you can check the property:

[self isMovingFromParentViewController]

to find out if the view is disappearing as a result of being popped off the stack or not.