美文网首页
9.4、通知中心

9.4、通知中心

作者: 艾希_可可 | 来源:发表于2018-06-27 12:05 被阅读6次

import UIKit

class OneViewController: UIViewController {
// 定义通知的名称
let refreshTableView = "refreshTableView"
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.title = "首页"
self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blue]
// 接收完成要移除观察者(deinit:相当于OC中的dealloc)
// NotificationCenter.default.removeObserver(self, name: (NSNotification.Name(rawValue: refreshTableView)), object: nil)
// 为通知添加观察者(接收者)
NotificationCenter.default.addObserver(self, selector: #selector(test2(_:)), name: NSNotification.Name(rawValue: refreshTableView), object: nil)

    let btn = UIButton(frame: CGRect(x: 100, y: 200, width: 100, height: 40))
    self.view.addSubview(btn)
    btn.backgroundColor = UIColor.gray
    btn.setTitle("Touch Me", for: .normal)
    btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
}
@objc func btnAction(){
    print("点击按钮发送通知")
    let name = "Tina"
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: refreshTableView), object: name)
}
func test2(_ info:Notification) {
    print("一页面收到通知\(info.object!)")
}

}

import UIKit

class FourViewController: UIViewController {
    let refreshTableView = "refreshTableView"
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red
        self.navigationController?.navigationBar.topItem?.title = "我的"
        
        NotificationCenter.default.addObserver(self, selector: #selector(test2(_:)), name: NSNotification.Name(rawValue: refreshTableView), object: nil)

    }
    func test2(_ info:Notification) {
        print("四页面收到通知\(info.object!)")
    }

相关文章

  • 9.4、通知中心

    import UIKit class OneViewController: UIViewController {/...

  • 班级开学通知9.4

    在无特殊情况的前提下,周一(9月6日)开学,请家长关注本群。请家长提醒孩子们准备好以下作业及学习用品: 作业: 1...

  • 通知中心

    ** 通知机制(消息机制)是一个应用程序级别的操作UIApplication 通知中心实际上是iOS程序内部之间的...

  • 通知中心

    通知中心 通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotifica...

  • 通知中心

    一、主线程开启通知中心 在主线程发送通知是同步的,执行顺序 before、ing、after。 在dealloc中...

  • 通知中心

    每个应用中只会有一个通知中心对象:NSNotificationCenter,通过单例方法,创建唯一对象,通知中心对...

  • NSNotification-通知

    通知 通知中心 NSNotificationCenter 通知中心的作用 添加观察者 通知的发送 给通知找到对应的...

  • 通知相关

    通知中心 通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotifica...

  • iOS 通知 与 通知中心

    iOS 通知中心:自己实现了一套消息机制,可以跨页面调用 类似Unity的SendMessage,是订阅、发布者模...

  • NotificationCenter 通知中心

    经测试发现1,对于默认的通知中心 NotificationCenter.default, 没有移除监听通知的情况...

网友评论

      本文标题:9.4、通知中心

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