简体   繁体   English

如何检查视图 controller 是否已在 Swift 中被取消

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

If I present a ViewController like so:如果我像这样呈现一个ViewController

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.我想知道ViewController何时被解雇。 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.这个ViewController不是我创建的,所以我无法更改viewWillDissapear方法。

Whole answer is predicated on an assumption that OP doesnt have access to authViewController code整个答案基于 OP 无权访问authViewController代码的假设

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.如果您无权访问authViewController代码,糟糕的解决方案是使用您的视图 controller 的viewWillAppear来查找何时关闭身份验证视图 controller。

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.基本上,当您在现有视图 controller 上呈现/推送任何视图控制器时,当呈现/推送视图 controller 被解除或弹出viewWillAppear时,您的视图控制器的viewWillDisappear将被类似地调用。

Because viewWillAppear might get called for other reasons as well and you wouldnt wanna confuse it as authViewController dismiss, use a boolean因为viewWillAppear也可能因为其他原因而被调用,并且您不想将其混淆为authViewController关闭,请使用 boolean

private var shouldMonitorAuthViewControllerDismiss = false //declared a instance property

Set the boolean to true when you actually present the authViewController当您实际呈现authViewController时,将 boolean 设置为 true

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

Finally in your viewWillAppear终于在你viewWillAppear会出现

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视图控制器被解雇后如何显示警报 - How to present an alert after view controller has been dismissed 模态视图控制器被关闭后如何调用函数 - How to call a function when a Modal View Controller has been dismissed ECSlidingViewController中的Top View Controller是否有办法知道边栏菜单何时被关闭? - Is there a way for the Top View Controller in ECSlidingViewController to know when the sidebar menu has been dismissed? 检查视图控制器是否已成功关闭动画 - Check that a view controller successfully dismissed with animation 如何可靠地检测到UIViewController已被解除 - How to reliablly detect that a UIViewController has been dismissed 如何检查单元格是否已显示Swift - How to check if a cell has been displayed Swift 在继续执行代码之前,如何检查UIAlertController是否已被关闭? - How do I check if UIAlertController has been dismissed before continuing to execute code? iOS视图控制器内存在被解除后未释放 - iOS view controller memory not released after it's been dismissed iOS - 了解模态视图何时被解除 - iOS - Know when modal view has been dismissed 如何测试视图控制器是否被关闭或弹出 - How to test if view controller is dismissed or popped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM