iOS-通知传值

第一界面:

     //通知中心是个单例
     NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];

     // 注册一个监听事件。第三个参数的事件名, 系统用这个参数来区别不同事件。
     [notiCenter addObserver:self selector:@selector(receiveNotification:) name:@"cesuo" object:nil];

     // @selector(receiveNotification:)方法, 即受到通知之后的事件
     - (void)receiveNotification:(NSNotification *)noti
{
   
     // NSNotification 有三个属性,name, object, userInfo,其中最关键的object就是从第三个界面传来的数据。name就是通知事件的名字, userInfo一般是事件的信息。
    NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);

 }
     
     // 第一界面中dealloc中移除监听的事件
- (void)dealloc
{
    // 移除当前对象监听的事件
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
    
}

第二界面:

     // 创建一个通知中心
         NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
     
     // 发送通知. 其中的Name填写第一界面的Name, 系统知道是第一界面来相应通知, object就是要传的值。 UserInfo是一个字典, 如果要用的话,提前定义一个字典, 可以通过这个来实现多个参数的传值使用。

         [center postNotificationName:@"cesuo" object:@"zhangheng" userInfo:dic];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容