我有一个计时器,当完成后会显示一个警告。
这个警告视图应该在用户当前所在的视图控制器中呈现。
我觉得这可以比以下方法更有效地实现:
现在我正在做的方式是为我的五个视图控制器中的每个都提供一个观察者用于通知以及一个用于创建并呈现该警告的方法。
是否有一种方法只设置一次警告,然后在当前活动的视图控制器中呈现它?
以下是我的代码:
非常感谢您提供的任何想法!
我觉得这可以比以下方法更有效地实现:
现在我正在做的方式是为我的五个视图控制器中的每个都提供一个观察者用于通知以及一个用于创建并呈现该警告的方法。
是否有一种方法只设置一次警告,然后在当前活动的视图控制器中呈现它?
以下是我的代码:
// I've got the following in each of my view controllers.
// In viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SonglistViewController.presentSleepTimerFinishedAlert(_:)), name: "presentSleepTimerFinishedAlert", object: nil)
}
func presentTimerFinishedAlert(notification: NSNotification) {
let alertController = UIAlertController(title: "Timer finished", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
非常感谢您提供的任何想法!