我尝试使用 NotificationCenter.addObserver(...)
打开本地通知时启用进入特定 View 的功能
我的代码是
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(...)
方法中的本地通知的名称?
最佳答案
您需要为通知名称创建扩展名,例如,
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
关于ios - 通知中心 addObserver() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47745700/