[英]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.