iOS 学会使用通知(notification)
来源:互联网 发布:iphone设计软件 编辑:程序博客网 时间:2024/05/19 17:59
继之间我们讲的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那么直观,不利于代码的维护,项目中我是尽量少用
- iOS 学会使用通知(notification)
- 使用通知(Notification)
- iOS通知Notification传值的使用
- Ios本地通知Notification的使用
- NOtification(通知)的使用
- android 使用通知(Notification)
- 8.1 使用通知(Notification)
- Android 使用通知(Notification)
- notification(通知)的使用
- notification(通知)的使用
- iOS 中常用通知(Notification)
- iOS之Local Notification(本土通知)
- IOS Notification通知实例
- IOS Notification 通知中心
- IOS Notification 通知中心
- iOS 通知notification
- iOS Notification 通知中心
- iOS开发 -- 通知Notification
- 协方差的意义和计算公式
- log4J 日志信息log4j.properties配置说明
- *-servlet.xml解析
- LeetCode-13-Roman to Integer(number/模拟)-Easy
- 抽象类和接口小结
- iOS 学会使用通知(notification)
- EBS Form个性化的工作原理
- Polya定理
- 一道面试题引发的对android中context的研究(一)-SharedPreference是否可以将文件保存在SD卡上
- php项目打包
- 瀑布流
- 怎样成为一个好的技术领导者
- C库函数 - strcmp()
- pl/sql developer 编码格式设置(解决pl/sql乱码、提示编码不统一)