使用NSNotificationCenter 事件通知
来源:互联网 发布:博思数据 电子烟 编辑:程序博客网 时间:2024/05/18 00:34
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFuckme:) name:strFuckMe object:nil];
第一个参数是要接收Notification的对象,第二个参数是响应函数,第三个是通知的名称,第四个参数表示接收哪个发送者的通知,如果第四个参数为nil,接收所有发送者的通知。
(2)既然init里面注册了这个通知,那么就需要在dealloc里面移除这个通知的注册:
代码如下:
[[NSNotificationCenter defaultCenter] removeObserver:self name:strFuckMe object:nil];
(3)怎么发送Notification通知呢?
代码如下:
如果不需要传递参数的通知:
[[NSNotificationCenter defaultCenter]postNotificationName:strFuckMe object:nil];
如果要传递参数的通知:
SFuckRet sFuckRet;
NSValue* value = [NSValuevalueWithBytes:&sFuckRetobjCType:@encode(SFuckRet)];
[[NSNotificationCenter defaultCenter] postNotificationName:strFuckMe object:nil userInfo:[NSDictionarydictionaryWithObjectsAndKeys:value,@"value",nil]];
SFuckRet是一个定长的结构体对象,所以需要用NSValue来包装以下,才能放到Object-c的容器中。
postNotificationName参数是要发送的通知的名字,object:参数是一个id,一般可以传递self,可以让接收通知者能调用的发送通知者。
userInfo是一个NSDictionary可以传递自己的其他信息。一般数据都放在这个里面。
注意一般NSValue一般用来包装定长的结构体,CGRect,CGPoint什么的,千万不能包含指针之类的东西。
(4)如何处理通知呢?
代码如下:
- (void)onFuckme:(NSNotification*)notification
{
NSDictionary* user_info = [notification userInfo];
NSValue* value = [user_info objectForKey:@"value"];
SFuckRet sfuckRet;
[value getValue:&sfuckRet];
}
NSDictionary* user_info = [notification userInfo]; 这句代码就是取出PostNitifycation的时候设置的NSDictionary信息。
这样就取出来了所要传递的结构体啦。
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- NSNotificationCenter 通知的使用
- NSNotificationCenter 通知的使用
- 通知中心NSNotificationCenter的使用
- iOS通知NSNotificationCenter的使用
- ios NSNotificationCenter通知的使用
- 快速使用 NSNotificationCenter iOS通知
- iOS使用NSNotificationCenter通知崩溃
- Swift - 使用NSNotificationCenter发送通知,接收通知
- NSNotificationCenter 通知
- 通知NSNotificationCenter
- 通知--NSNotificationCenter
- 通知NSNotificationCenter
- NSNotificationCenter通知
- 使用通知中心NSNotificationCenter遇到的问题
- 由Cannot create a generic array of ArrayList<xx>引出的学习--Java范型
- Java Web培训课程一期项目视频发布
- UEditor1.4.3 使用配置心得
- 负数-补码
- PHP基础知识总结
- 使用NSNotificationCenter 事件通知
- Opencv Mat类
- Java中合理的停止掉一个线程thread
- setBackground和setBackgroundDrawable的区别
- BAT解密:互联网技术发展之路(1) - 技术发展的驱动力
- C#与C++的区别
- Activity 知识点
- 移动端页面性能探究
- Win7/8下提示OpenSCManager failed 拒绝访问