ios - 通知中心 addObserver() 问题

标签 ios swift uilocalnotification

我尝试使用 NotificationCenter.addObserver(...) 打开本地通知时启用进入特定 View 的功能

我的代码是

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

使用 test 方法执行到带有标识符 NotificationViewViewController 的连接,我不确定要放什么...

@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/

相关文章:

ios - UIScrollView 缩放仅用 2 个手指

objective-c - 从通知中心删除 UILocalNotifications

ios - Swift 默认的 AlertViewController 打破约束

ios - iTunes 恢复到旧版本

swift - 类型 'SecondPageController' 的值没有成员 'miniView'

ios - swift 。设置文本字段中输入的格式

ios - AppDelegate 中的本地通知问题

iphone - 基于位置的本地通知

ios - Facebook-unity-sdk 不使用 iOS 原生 SSO

ios - 使用 iOS 在 XMPP 中传输文件的服务不可用 503 错误