ios - NotificationCenter swift3 无法观察帖子

标签 ios swift swift3 nsnotificationcenter notificationcenter

我有 3 个通知:

NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification3"), object: nil)

我在 View Controller 中为每个帖子设置了一个不同的观察者

首先:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification1"), object: nil, queue: nil, using: updateUx)

第二个:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification2"), object: nil, queue: nil, using: updateUx)

第三:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification3"), object: nil, queue: nil, using: updateUx)

updateUx 函数仅包含通知打印。

我只收到我的第一个通知我无法捕捉到另外两个,我不知道为什么。

最佳答案

像下面这样添加你的观察者:

NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)

一切顺利。


编辑: 完整的源代码(这个项目有一个 UIButton View 和 @IBAction 在 Storyboard 中连接到它。点击那个按钮,将发布所有 3 个通知。日志应该在控制台中打印三次)

class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }

    @IBAction func abc (_ sender: UIButton) {
        NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil)
        NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil)
        NotificationCenter.default.post(name:NSNotification.Name("Notification3"), object: nil)
    }

    func updateUx(){
        print("received...")
    }
}

关于ios - NotificationCenter swift3 无法观察帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436651/

相关文章:

ios - UITableView 的 ScrollView 插入在 iOS 13 设备中未调整

swift - 将 Facebook 登录与 Parse Swift 3 集成

ios - UnsafeMutablePointer 初始化

ios - 强引用 `self` 使对象保持事件状态(暂时) : evil?

swift - Apple TV 上带有 swift 的 .XZ 文件

ios - UIFont 自定义样式设置

ios - 添加配件类型在 UITableViewCell 中移动标签

swift - Alamofire 之后解析 JSON

ios - 在 UISplitView 中替换我的 detailViewController 在旋转我的应用程序 Objective-C 时崩溃

ios - JSON 和 iOS 以及字符串数据