我正在尝试使用NotificationCenter.addObserver(...)在打开本地通知时启用分段到特定视图
我的代码是
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(LocalNotificationViewController.test), name: ???, object: nil)
}使用test方法执行到带有标识符NotificationView的ViewController的分段,我不确定应该放什么……
@objc func SomeNotificationAct(notification: NSNotification){
DispatchQueue.main.async() {
self.performSegue(withIdentifier: "NotificationView", sender: self)
}
}我的主要问题是如何知道要放入NotificationCenter.addObserver(...)方法的本地通知的名称?
发布于 2017-12-11 13:46:40
您需要为通知名称创建扩展名,如
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
https://stackoverflow.com/questions/47745700
复制相似问题