如果我调用(在 UIViewController
中)
alert.dismiss(animated: false, completion: nil)
print("executed after dismiss?")
鉴于警报是先前呈现的 UIAlertController
,dismiss 方法是否同步执行?
我能做吗:
alert.dismiss(animated: false, completion: nil)
let newAlert = UIAlertController(...)
present(newAlert, animated: true, completion: nil)
在呈现 newAlert
时不必担心问题?
最佳答案
与大多数动画方法一样,当您关闭 UIAlertController
时,您只是让球滚动。 View Controller 被异步关闭和删除。为了测试这一点,我们可以使用如下代码:
let alert = UIAlertController(title: "Oh No!", message: ":(", preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)
print(presentedViewController) // Optional(<UIAlertController: 0x7fefe901e670>)
alert.dismiss(animated: true, completion: nil)
print(presentedViewController) // Optional(<UIAlertController: 0x7fefe901e670>)
如上所示,警报 View Controller 并没有立即作为呈现的 View Controller 被删除。您仍然可以在关闭旧警报后立即显示新警报。但是,最佳做法是将第二个警报的代码放在第一个警报的完成处理程序中。在呈现的 View Controller 上调用 viewDidDisappear
之后调用此完成处理程序。
关于ios - 没有动画的UIAlertController的dismiss方法是否同步执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41943817/