NotificationCenter addObserver()问题

时间:2017-12-11 02:34:17

标签: ios swift uilocalnotification

我尝试使用NotificationCenter.addObserver(...)打开本地通知时将segue启用到特定视图

我的代码是

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(LocalNotificationViewController.test), name: ???, object: nil)
}

使用test方法执行带有标识符ViewController的{​​{1}}的segue,我不知道该放什么......

NotificationView

我的主要问题是如何知道我的本地通知的名称?{/ 1}}方法?

1 个答案:

答案 0 :(得分:0)

您需要为通知名称创建扩展名,例如

extension Notification.Name {

     static let hello1 = Notification.Name("HelloNotifcationName")
     static let hello2 = Notification.Name("HelloNotifcationName2")

}

并像这样使用它,

NotificationCenter.default.addObserver(self, selector: #selector(setToHelloName1(notification:)), name: .hello1, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(setToHelloName2(notfication:)), name: .hello2, object: nil)

您可以找到更多参考或教程,here