简体   繁体   中英

How to reliablly detect that a UIViewController has been dismissed

I need to do things when a ViewController is dismissed, ie:

  • When “Back” is pressed
  • When popToRootViewController is called by a parent if was in a UINavigationController
  • when DismissViewController is called by a parent if it was presented

eg I need to unsubscribe from events, or dimiss a presented alert etc.

ViewWillDisappear is not called when popToRootViewController is called by it's parent so that doesnt work.

WillMoveToParentViewController is only for View Controller containment

Dealloc is no good as that is only when garbage collected. I'm using c# Xamarin anyway and it doesnt work there.

ViewDidUnload is no longer used and was never for this anyway

A ViewController itself should not care how it was presented so how do we find out when it is dismissed?

This seems like an important, basic requirement. How do we unsubsribe to events from a model without this, for example?

This is a similar question, but no good answer:

Can you detect when a UIViewController has been dismissed or popped? ?

This question is a bit old but here is the conclusion I came to:

-dealloc - no guarantees on when this is going to get called. Good for boilerplate code but not reliable.

-viewDidDisappear & -viewWillDisappear are only called when the view stops being visible (imagine that ;) ). So they are ok when popping one view at a time but not for popToRoot or popping back several controllers at a time

-willMoveToParentViewController should work (viewControllers in a UINavigation Controller are contained by that controller) but for some reason I'm seeing inconsistencies on when this gets called or not.

-didMoveToParentViewController - does get called on every viewController being dismissed from the navigation stack. So... this looks like the most reliable place to do this.

我从我的模态中调用了presentsViewController.viewWillAppear来让它知道它被解雇了。

When using a UINavigationController it is available through the NavigationController property on the UIViewController (or equivalent) subclass object.

The ViewWillDisappear(bool animated) and ViewDidDisappear(bool animated) will be called when a new UIViewController is loaded onto the UINavigationController stack.

It may help if you utilise the storyboard functionality within Xamarin Studio as the NavigationController->UIViewController relationship is visually represented.

If you are having further trouble please post sample code in order for the community to review further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM