iOS通知的整理笔记

iOS通知用于高耦合界面的传值确实方便快捷。

需要实现模态弹出的视图控制器上,有一个视图控制器可以导航。这必定要将这个视图控制器的导航视图控制器naVC.view添加到模态弹出的视图控制器presentedVC.view上。

如何把naVC上的二级视图控制器上的值传给presentedVC?

解决方法:使用通知

1、在需要传值的naVC二级视图控制器界面创建一个通知

//创建通知
    NSNotification *notification = [NSNotification notificationWithName:@"sendSelectTitle" object:nil userInfo:@{@"key":value}];

2、通过通知中心发送通知

//通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];

3、在需要接收值得presentedVC界面的viewDidLoad中通过通知中心注册这条通知的观察者和绑定执行方法

//注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(make:)name:@"sendSelectTitle" object:nil];

4、已经注册过的对象如果不需要接收信息时,在通知中⼼注销(写在presentedVC模态消失时)

[self dismissViewControllerAnimated:NO completion:^{
            // 注销通知
            [[NSNotificationCenter defaultCenter]removeObserver:self name:@"sendSelectTitle" object:nil];
        }];

********************系统通知不需要通过通知中心发送************************

原文地址:https://www.cnblogs.com/xs514521/p/5645898.html