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];