iOS通知中心的使用

实现通知中心传值分为四步:
1、第一步,我们在需要接收通知的地方注册观察者
2、第二步,在需要时发送通知的地方传值
3、第三步,实现回调的函数
4、第四步,移除通知中心
一、OC中通知中心的实现

- (void)viewDidLoad {
    [super viewDidLoad];
    // 第一步,我们在需要接收通知的地方注册观察者
    // 获取通知中心单例对象 添加一个观察者 传一个值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passNotificationAction:) name:@"pass" object:nil];
    // 传多个值
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passDicNotificationAction:) name:@"passDic" object:nil];
}
- (IBAction)buttonAction:(UIButton *)sender {
    // 第二步,在需要时发送通知的地方传值
    // 传一个值
    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:@"pass" object:@"this is notice" userInfo:nil]];
    // 传多个值
    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:@"passDic" object:nil userInfo:@{@"string":@"123456789"}]];
}
// 第三步,实现回调的函数
// 传一个值事件
-(void)passNotificationAction:(NSNotification *) sender {
    NSLog(@"%@",[sender object]);
}
// 传多个值事件
-(void)passDicNotificationAction:(NSNotification *) sender {
    NSLog(@"%@",[[sender userInfo] objectForKey:@"string"]);
}
// 第四步,移除通知中心
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // 移除指定的观察者
    // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"pass" object:nil];
}

二、Swift中通知中心的实现

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 第一步,我们在需要接收通知的地方注册观察者
        //单个值得传递
        NotificationCenter.default.addObserver(self, selector: #selector(self.passNotificationAction(sender:)), name: NSNotification.Name(rawValue: "pass"), object: nil)
        //多个值得传递
        NotificationCenter.default.addObserver(self, selector: #selector(self.passDicNotificationAction(sender:)), name: NSNotification.Name(rawValue: "passDic"), object: nil)
    }

    @IBAction func buttonAction(_ sender: UIButton) {
        // 第二步,在需要时发送通知的地方传值
        // 单值传递
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pass"), object: "this is notice", userInfo: nil)
        // 多个值得传递
        let dic: [String: Any]  = ["string": "123467890"]
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "passDic"), object: nil, userInfo: dic)
    }

    // 第三步,实现回调的函数
    //单个值得回调方法
    func passNotificationAction(sender: Notification)  {
        print(sender.object as Any)
    }
    //多个值得回调方法
    func passDicNotificationAction(sender: Notification)  {
        if let dic = sender.userInfo as? Dictionary<String, Any> {
            print(dic["string"] as Any)
        }
    }

    // 第四步,移除通知中心
    deinit {
        // 移除所有的通知(在哪里创建的通知,就在哪里移除)
        NotificationCenter.default.removeObserver(self)
        //移除指定的通知
        // NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "passValue"), object: nil)
        // NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "pass"), object: nil)
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值