美文网首页
通知NotificationCenter的使用

通知NotificationCenter的使用

作者: FallPine | 来源:发表于2018-09-14 17:17 被阅读10次
  • 发送通知

let notificationName = Notification.Name.init("DownloadImageNotification")
NotificationCenter.default.post(name: notificationName, object: self,
                                                        userInfo: ["value1":"hangge.com", "value2" : 12345])
  • 订阅通知

NotificationCenter.default.rx
            .notification(Notification.Name.init("DownloadImageNotification"))
            .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { notification in
                //获取通知数据
                let userInfo = notification.userInfo as! [String: AnyObject]
                let value1 = userInfo["value1"] as! String
                let value2 = userInfo["value2"] as! Int
                print("获取到通知,用户数据是[\(value1),\(value2)]")
                //等待3秒
                sleep(3)
                print(" 执行完毕")
            }).disposed(by: disposeBag)

参考文章:Swift - RxSwift的使用详解63(通知NotificationCenter的使用)

相关文章

网友评论

      本文标题:通知NotificationCenter的使用

      本文链接:https://www.haomeiwen.com/subject/yiaogftx.html