iOS swift3.0 广播通知Broadcast notification 的使用

前言:广播通知属于是观察者模式。例如A想要在Button按下的时候收到通知,那么A就是观察者了。要给A发通知,就需要先定义一个"Button通知",Button被按下的时候发出通知。A需要接收“Button通知”就要先注册,不单是A,可以是B,也可以是C,哪个类需要接收这个通知都可以注册,成功注册“Button通知”就可以在需要的时候收到消息了。

 

 广播通知Broadcast notification的使用:

第一步: 定义一个全局变量初始化广播名   

let exampleNotification = NSNotification.Name(rawValue: "ExampleNotification")

 

第二步:在需要监听通知的类注册通知并声明接收通知的函数

方法一(注册通知与接收通知分开)

注册通知

//observer:监听通知的对象
//selector:接收通知的方法
//name:通知名
//object:投递通知的对象,如果不为nil则只接收该对象发过来的通知
 NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.example(_:)), name:exampleNotification , object: nil)

 

接收通知的方法

 func example(_ notification: Notification) {

       //接收到通知了!!做点什么吧
       let info = notification.userInfo //获取通知的信息

}

 

方法二(采用闭包方式)

注册和接收通知       

//name: 通知的名字
//object: 投递通知对象,如果为nil则不管投递通知的对象是什么,只要该通知来了就接收
//queue: 队列,决定block在哪个线程执行,nil表示在发布通知的线程中执行
//using: 只要监听到就会执行这个block
NotificationCenter.default.addObserver(forName:exampleNotification , object: nil, queue: nil)
{ (notification) in

      //接收到通知了!!做点什么吧
      let info = notification.userInfo //获取通知的信息
}

 

第三步:注销通知(在接收通知对象中注销,可在-viewDidDisappear方法中实现) 

1.注销指定对象的指定通知       

// observer:  接收通知的对象,通常为self
// name:通知名,根据投递通知的名字注销指定通知
// object:投递通知的对象,不为nil则可以根据投递通知的对象注销指定通知        
NotificationCenter.default.removeObserver(self, name: exampleNotification , object: nil)

 

2. 注销指定对象的所有通知        

NotificationCenter.default.removeObserver(self)

 

第四步:(在通知投送对象)发送通知

1. 发送带有userInfo的通知     

// object:投递通知的对象
//userInfo:  投送通知时定义的字典对象
let dataDict = ["userName": data]
NotificationCenter.default.post(name: exampleNotification, object: nil, userInfo: dataDict)

 

2. 发送不带有userInfo的通知     

// object:投递通知的对象
//userInfo:  投送通知时定义的字典对象
let dataDict = ["userName": data]
NotificationCenter.default.post(name: exampleNotification, object: nil)

 

实用例子(注册界面发送通知给登录界面):

登录界面的类

class loginViewController: UIViewController {

    let didRegisterNotification = NSNotification.Name(rawValue: "DidRegisterNotification")

    override func viewDidLoad() {
        super.viewDidLoad()

        //投送通知时指定对象了,注册通知也必须指定投送对象才能接受到通知
        NotificationCenter.default.addObserver(forName:didRegisterNotification , object: registerViewController.self, queue: nil) { (notification) in

        //接收到通知了!!做点什么吧
        let info = notification.userInfo //获取通知的信息

        }

    }

deinit{
         //注销通知
         NotificationCenter.default.removeObserver(self)
    }

}

 

注册界面的类

class registerViewController: UIViewController {

 override func viewDidLoad() {

        super.viewDidLoad()

        //投递通知,指定投送通知对象
        NotificationCenter.default.post(name: didRegisterNotification , object:registerViewController.self)

    }

}

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值