【发布时间】:2017-08-11 16:44:46
【问题描述】:
我想在任何 UIAlertController 由于用户点击警报按钮之一而自行关闭(动画完成)之前和之后立即执行一些操作并呈现一些 UI。
如何在用户按下我的 UIAlertController 中的某个按钮并且它将被解除然后被解除时收到通知?
在文档中建议不要继承 UIAlertController。我仍然尝试了我的运气子类化,认为它可能在内部调用func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 本身。 self.dismiss(... 之类的东西,但在 iOS10 上似乎不是这样。
我还尝试将“手动”解除添加到 UIAlertAction 处理程序中:
let alert = UIAlertController.init(...
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
alert.dismiss(animated: true, completion: {
print("Dismissed")
})
})
alert.addAction(defaultAction)
但似乎在按下按钮后但在调用处理程序之前解除了警报。无论如何它也不起作用。即使它有效,记住将我的代码添加到每个 UIAlertAction 处理程序中也会有点麻烦。
我会很感激任何想法。
【问题讨论】:
-
为什么你不在“defaultAction”块中做所有需要的事情?无论如何,此阻止警报将被解除。
-
@biloshkurskyi.ss 正如我在我的问题中所写的那样 - 它容易出错,很容易忘记某处的块。
标签: ios uiviewcontroller uialertcontroller