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??
When using a UINavigationController it is available through the NavigationController property on the UIViewController (or equivalent) subclass object.
The
ViewWillDisappear(bool animated)
andViewDidDisappear(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.