简体   繁体   中英

How to check if a view controller has been dismissed in Swift

If I present a ViewController like so:

let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: nil)

I would like to know when the ViewController has been dismissed. I have tried the following:

let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: {
   print("View Dismissed")
})

but that only lets me know if the view was presented successfully. This ViewController was not created by me so I can't change the viewWillDissapear method.

Whole answer is predicated on an assumption that OP doesnt have access to authViewController code

If you dont have access to authViewController code, lousy solution would be to use viewWillAppear of your view controller to find when auth view controller is dismissed.

Basically when you present/push any viewController over your existing view controller, your view controller's viewWillDisappear will be called similarly when presented/pushed view controller is dismissed, or popped out viewWillAppear will be called.

Because viewWillAppear might get called for other reasons as well and you wouldnt wanna confuse it as authViewController dismiss, use a boolean

private var shouldMonitorAuthViewControllerDismiss = false //declared a instance property

Set the boolean to true when you actually present the authViewController

let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: {
    shouldMonitorAuthViewControllerDismiss = true
})

Finally in your viewWillAppear

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        if shouldMonitorAuthViewControllerDismiss {
            //auth view controller is dismissed
        }
        shouldMonitorAuthViewControllerDismiss = false
    }

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