Can you detect when the UIViewController got fired or popped up?

I have some sort of cleanup that needs to be done on the share anytime one of my view controllers gets rejected / unloaded / unloaded? This could be either when the user clicks the Back button on that separate screen, or when popToRootViewController is called (in which case, I would ideally be able to clear every controller that got knocked out.)

The obvious choice would be to do this in viewDidUnload, but of course this is not how unloading works. Is there a way to catch all cases where the ViewController is removed from the stack?

edit: Forgot to mention that I am doing this with Xamarin so that it may or may not affect the answers.

+7


source to share


3 answers


override func viewDidDisappear(animated: Bool) {
    if (self.isBeingDismissed() || self.isMovingFromParentViewController()) {
        // clean up code here
    }
}

      

UPDATE for fast 4/5



override func viewDidDisappear(_ animated: Bool) {
    if (self.isBeingDismissed || self.isMovingFromParent) {
        // clean up code here
    }
}

      

+15


source


-dealloc

is probably your best bet. The view controller will be deallocated if it popped off the stack, unless you saved it elsewhere.

viewWillDisappear:

and viewDidDisappear:

are not a good choice because they are called anytime the view controller is no longer visible, including when it pushes something else on the stack (which is why it becomes second from the top).



viewDidUnload

is no longer used. System environments stopped naming this method as of iOS 6.

+5


source


-(void) viewDidDisappear:(BOOL)animated{

} 

      

-3


source







All Articles