在IOS中,每一个运行中的程序都有个NSNotificationCenter(通知中心)成员变量。如果有对象需要关注某一个NSNotification(通知),
首先去通知中心注册
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(todoOne) name:@"Method" object:nil];
其中self就是关注某一通知的对象,也叫Observer;@"Method"指的是Notification的名字;@selector里面的方法todoOne就是这个对象接受到通知(Method)的时候该执行的动作;最后一个参数指的是发送通知的对象,如果发送通知的对象为nil,则通知中心将所有名字为@“Method”的NSNotification转发给关注这个通知的对象self。
其次,在需要的时候通知中心发送通知,这个时候,关注Method的对象就会调用todoOne方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"Method" object:self];
最后移除关注Method的对象[[NSNotificationCenter defaultCenter] removeObserver:self];
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(todoOne) name:@"Method" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(todoTwo:) name:@"two" object:nil]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void)todoOne{ NSLog(@"todoOne is called"); } - (void)todoTwo:(NSNotification *)object{ NSLog(@"todoTwo: is called %@",object); NSNumber *integer = [[object userInfo] objectForKey:@"sum"]; NSLog(@"the received is %d",[integer intValue]); } - (IBAction)sendNotification1:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@"Method" object:self]; } - (IBAction)sendNotification2:(id)sender { NSDictionary *mDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:5] forKey:@"sum"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"two" object:self userInfo:mDictionary]; } - (IBAction)removeNotification:(id)sender { [[NSNotificationCenter defaultCenter] removeObserver:self]; NSLog(@"remove Notification"); } @end
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;
如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给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;
参考博客:
http://blog.sina.com.cn/s/blog_5df7dcaf0100c0q2.html
http://blog.csdn.net/ch_soft/article/details/6682809