NSNotificationCenter 的使用

NSNotificationCenter 的使用,第1张

概述NSNotificationCenter 的使用 1,定义一个方法 -(IBACtion)shuchu{  } 2,对象注册,并附带信息[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shuchu) name:@"Method" object:nil] 3,发送通知信息 [[NSNotificati

NSNotificationCenter 的使用

1,定义一个方法

-(IBACtion)shuchu{  }

2,对象注册,并附带信息[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(shuchu) name:@"Method" object:nil]

3,发送通知信息

[[NSNotificationCenter defaultCenter] postNotificationname:@"Method" object:nil];


什么是Notification?

这个要求其实也很容易实现每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). center将该notifications转发给所有注册对该notification感兴趣的对象我们把这些发送notification的对象叫做 poster

很多的标准Cocoa类会发送notifications: 在改变size的时候,Window会发送notification; 选择table vIEw中的一行时,table vIEw会发送notification;我们可以在在线帮助文档中查看到标准cocoa对象发送的notification

在对象释放前,我们必须从notification center移除我们注册的observer. 一般我们在dealloc方法中做这件事




NSNotification

提供给observer的信息包裹. notification对象有两个重要的成员变量: name  object.

- (Nsstring *)name;

- (ID)object;

- (NSDictionary *)userInfo;我们想要notification对象传递更多的信息


+ (ID)notificationWithname:(Nsstring *)aname object:(ID)anObject;

+ (ID)notificationWithname:(Nsstring *)aname object:(ID)anObject userInfo:(NSDictionary *)aUserInfo;




NSNotificationCenter

+ (ID)defaultCenter;返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]

    

- (voID)addobserver:(ID)observer selector:(SEL)aSelector name:(Nsstring *)aname object:(ID)anObject;

 如果notificationnamenil. 那么notification centeranObject发送的所有notification转发给observer

如果anObjectnil.那么notification center将所有名字为notificationnamenotification转发给observer


- (voID)postNotification:(NSNotification *)notification;

- (voID)postNotificationname:(Nsstring *)aname object:(ID)anObject;

- (voID)postNotificationname:(Nsstring *)aname object:(ID)anObject userInfo:(NSDictionary *)aUserInfo;


- (voID)removeObserver:(ID)observer;

- (voID)removeObserver:(ID)observer name:(Nsstring *)aname object:(ID)anObject;

总结

以上是内存溢出为你收集整理的NSNotificationCenter 的使用全部内容,希望文章能够帮你解决NSNotificationCenter 的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066499.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存