在工程的两个地方注册通知, 一个是AppDelegate, 模拟后台接受通知的处理, 一个是ViewController, 模拟UI接受通知的处理. 在另一个界面SecondViewController中设置一个按钮事件, 模拟通知的触发.
界面准备:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
ViewController中有一个button(进入下一页)和一个label(显示通知内容)
UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
[btn setTitle:@"进入下一页" forState:(UIControlStateNormal)];
[btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
btn.frame = CGRectMake(100, 100, 200, 30);
[btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btn];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 30)];
label.tag = 9090;
label.textColor = [UIColor blackColor];
label.text = @"------";
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
SecondViewController中有一个Button(按钮触发通知的发送)
UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
[btn setTitle:@"触发通知" forState:(UIControlStateNormal)];
[btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
btn.frame = CGRectMake(100, 100, 150, 30);
[btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:btn];
// 用按钮事件模拟触发通知
- (void)btnAction:(UIButton *)btn {
[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_LABELTEXT" object:@"第二页的通知内容"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"APPDELEGATE_NOTIFICATION" object:@"通知Applegate执行事件"];
}
注册两个通知中心:
AppDelegate.m
// 注册通知 self接受名为@"APPDELEGATE_NOTIFICATION"的通知后,执行notification:方法
// nil表示不限制通知的发送者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"APPDELEGATE_NOTIFICATION" object:nil];
// 接受对应通知之后执行的方法
- (void)notification:(NSNotification *)notification {
// notification.name 通知的name
// notification.object 通知的内容, 可自定义
NSLog(@"%@", notification.object);
}
ViewController.m
// 注册通知 self接受名为@"CHANGE_LABELTEXT"的通知后,执行notification:方法
// nil表示不限制通知的发送者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"CHANGE_LABELTEXT" object:nil];
// 接受对应通知之后执行的方法
- (void)notification:(NSNotification *)notification {
// notification.name 通知的name
// notification.object 通知的内容, 可自定义
NSString *str = (NSString *)notification.object;
((UILabel *)[self.view viewWithTag:9090]).text = str;
}
注意:每注册一个通知, 就要在相应类的dealloc方法体类注销通知:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
点击SecondViewController中的按钮时, 发送两个通知, 分别通知AppDelegate和ViewController进行打印和修改label的操作. 当然也可以一个通知多个地方监听, 执行不同操作.