关闭当前呈现的所有 UIAlertControllers [英] Dismiss all UIAlertControllers currently presented
问题描述
有没有办法关闭当前显示的所有 UIAlertControllers?
Is there a way to dismiss all UIAlertControllers that are currently presented?
这特别是因为从我的应用程序的任何位置和任何状态,我都需要在按下推送通知时到达某个 ViewController.
This is specifically because from anywhere and any state of my app, I need to get to a certain ViewController when a push notification is pressed.
推荐答案
您可以将 UIAlertController
子类化,将 NSNotification
观察者附加到每个将触发UIAlertController
子类以关闭警报控制器,然后在您准备关闭时发布 NSNotification
,例如:
You could subclass your UIAlertController
s, attach NSNotification
observers to each which would trigger a method within the UIAlertController
subclass to dismiss the alert controller, then post an NSNotification
whenever you're ready to dismiss, ex:
class ViewController: UIViewController {
func presentAlert() {
// Create alert using AlertController subclass
let alert = AlertController(title: nil, message: "Message.", preferredStyle: UIAlertControllerStyle.Alert)
// Add observer to the alert
NSNotificationCenter.defaultCenter().addObserver(alert, selector: Selector("hideAlertController"), name: "DismissAllAlertsNotification", object: nil)
// Present the alert
self.presentViewController(alert, animated: true, completion:nil)
}
}
// AlertController subclass with method to dismiss alert controller
class AlertController: UIAlertController {
func hideAlertController() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
然后在您准备关闭警报时发布通知(在本例中,当按下推送通知时):
Then post the notification whenever you're ready to dismiss the alert (in this case, when the push notification is pressed):
NSNotificationCenter.defaultCenter().postNotificationName("DismissAllAlertsNotification", object: nil)
这篇关于关闭当前呈现的所有 UIAlertControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!