使用NSNotificationCenter 事件通知
来源:互联网 发布:2003excel查找重复数据 编辑:程序博客网 时间:2024/06/05 23:12
[[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:strFuckMeobject: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遇到的问题
- JQuery实现二级 select框联动选择
- C#串口操作类 【转】
- Java异常详解
- frameset使用方法
- oracle开发中的nvl函数
- 使用NSNotificationCenter 事件通知
- Privacy Policy of the The apk
- Windows 8上搭建Cocos2d-x for Android的开发环境
- PostThreadMessage
- always和always@(*)
- FIQ和IRQ区别
- Perl之冒泡排序法
- 使用eclipse进行java web开发的三中tomcat部署方式
- OpenCV中的onMouse事件、ROI区域设置、直线斜率检测