iPhone开发之使用NSNotification(通知)

主要函数、方法

//
+ (id)defaultCenter; //增加通知事件 - (void)addObserver:(id)observer selector:(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)observer name:(NSString *)aName object:(id)anObject;

 

实例:

//在mainviewcontroller中添加观察者

 //接受编辑/完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(setEditButtonOn)
                                                 name:@"SETEDITBUTTONON"
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(setEditButtonOff)
                                                 name:@"SETEDITBUTTONOFF"
                                               object:nil];
//通知触发的事件
-(void)setEditButtonOn{
    self.editButton.selected = YES;
}

- (void)setEditButtonOff{
    self.editButton.selected = NO;
}
//发送编辑通知
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"SETEDITBUTTONON" object:nil];

//最后移除观察者
  [[NSNotificationCenter defaultCenter]removeObserver:self name:@"SETEDITBUTTONON" object:nil];

转载于:https://www.cnblogs.com/ios-wmm/archive/2012/09/11/NSNotification.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值