NSNotificationCenter 传递带参数的通知

前提是你 提交于 2020-02-11 05:26:30

NSNotificationCenter  在  init里面注册这个通知,

NSString* const str = @"FuckMe";

   [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(on:)   name:str object:nil];

在dealloc里面移除这个通知的注册: 

   [[NSNotificationCenter  defaultCenterremoveObserver:self  name:str object:nil];

以上为不带参数的通知

 

一般在使用NSNotificationCenter的时候不使用参数,但是有些时候需要使用参数。

传递参数

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];

接收参数并获取传递的参数

 

postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

object:传递的参数

 

- (void) test:(NSNotification*) notification

{

    searchFriendArrary = [notification object];//通过这个获取到传递的对象

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!