Notification

Notification


首先举一个小例子:

1.定义一个方法

- (void) update:(NSNotification *) aNotification
{
 
}

2.注册消息中心

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(update:)  name:@"RefreshNotification"object:nil];

3.发送一个消息

[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshNotification"object:self];

4.移除注册的消息

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshNotification"object:nil];


 --NSNotification 和NSNotificationCenter

Notification对象非常简单. 它就是poster要提供给observer的信息包裹. notification对象有三个重要的成员变量:

Name、object和userInfo.一般object都是指向poster

 

- (NSString *)name;          // 消息名称
- (id)object;                // 消息参数,可传对象
- (NSDictionary *)userInfo;  // 字典类型,可用于传递更多的参数

NSNotificaitonCernter是个控制台.它允许我们注册observer对象, 发送notification,撤销observer对象注册

下面是它的一些常用方法

 

/*
 *  类方法,返回全局变量,单利模式
 *  返回一个消息中心
 */
+ (id)defaultCenter;
   
 
/*
 *  注册一个消息
 *  observer:注册观察者的对象,不能为nil
 *  selector:事件处理的方法
 *  name:消息名称,用来识别消息
 *  object:消息发送者,只有这个消息发送者发送的消息才会被接受,nil时都接收
 */
- (void)addObserver:(id)observerselector:(SEL)aSelector name:(NSString*)aName object:(id)anObject;
 
/*
 *  发送一个消息包裹
 */
- (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)observername:(NSString *)aName object:(id)anObject;



UILocalNotification

本地消息,待写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值