继之间我们讲的delegate和block之后,今天我们一块来研讨一下notification,在实际开发中,我一般很少用到通知,因为如果项目中大量用到通知的话,就会大大降低效率,除非万不得已,跨界面之间的传递,因为notificationcenter就是一个单例,所以可以全局使用,也就是说只有在这个时候,我才会用到通知,下边我给一个demo,具体实现的现象就是我创建了三个controller,firstViewController,secondViewController,ThirdViewController,其中第二个界面就是一个起一个push的效果,为了能清晰的证明,通知可以跨界面之间进行传值(传事件),在firstViewController中注册一个观察者,并写一个弹窗事件,在thirdViewController中发送通知(在这点需要注意,那就是通知的名字一定一致,不然,肯定thirdViewController在发送通知的时候是找不到对应通知的,也就不会有回调事件了),(在需要做事情的地方发送通知,谁可以来做这件事情,谁就注册一个观察者来帮助发送这个通知的人实现这个事件),好了,就是这样,下边是demo:
firstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interfaceFirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorbrownColor];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(50,100,200, 50);
button.backgroundColor = [UIColorredColor];
[buttonsetTitle:@"push"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(showNotification:)name:@"notDemo"object:nil];
}
- (void)showNotification:(NSNotification *)notification {
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"title"message:@"message"preferredStyle:UIAlertControllerStyleAlert];
[alertaddAction:[UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:nil]];
[selfpresentViewController:alertanimated:YEScompletion:nil];
}
- (void)btnClick:(UIButton *)sender {
SecondViewController *second = [[SecondViewControlleralloc]init];
[self.navigationControllerpushViewController:secondanimated:YES];
}
@end
secondVIewController.m
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interfaceSecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorcolorWithRed:0.5green:0.6blue:0.2alpha:YES];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(50,100,200, 50);
button.backgroundColor = [UIColorredColor];
[buttonsetTitle:@"push"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)btnClick:(UIButton *)sender {
ThirdViewController *third = [[ThirdViewControlleralloc]init];
[self.navigationControllerpushViewController:thirdanimated:YES];
}
@end
thirdViewController.m
#import "ThirdViewController.h"
@interfaceThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorcolorWithRed:0.2green:0.6blue:0.5alpha:YES];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(50,100,200, 50);
button.backgroundColor = [UIColorredColor];
[buttonsetTitle:@"send notification"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)btnClick:(UIButton *)sender {
[[NSNotificationCenterdefaultCenter]postNotificationName:@"notDemo"object:niluserInfo:nil];
}
@end
但是使用通知就是不如代理,block那么直观,不利于代码的维护,项目中我是尽量少用