通知基本用法NSNotificationCenter
2016-07-14 14:04
225 查看
//第一个viewcontriller
@implementation testViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)postBtn:(id)sender {
//发送通知第一种方法:初始化一个通知在用通知中心发送
// NSNotification *nofi = [NSNotification notificationWithName:@"testnoti" object:nil userInfo:@{@"zyc":@"123"}];
// [[NSNotificationCenter defaultCenter]postNotification:nofi];
//发送通知第二种方法:直接通知中心发送 object和userinfo都是通知的属性
[[NSNotificationCenter
defaultCenter]postNotificationName:@"testnoti"
object:@"1"
userInfo:@{@"zyc":@"123"}];
}
//第二个viewcontriller
@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(aa:)
name:@"testnoti"
object:nil];
}
//执行操作
- (void)aa:(NSNotification *)sender{
NSLog(@"%@",sender);//可以利用通知属性 object userinfo 传值过来
}
- (IBAction)pushto:(id)sender {
testViewController *testvc = [[testViewController
alloc]init];
[self.navigationController
pushViewController:testvc animated:YES];
}
@implementation testViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)postBtn:(id)sender {
//发送通知第一种方法:初始化一个通知在用通知中心发送
// NSNotification *nofi = [NSNotification notificationWithName:@"testnoti" object:nil userInfo:@{@"zyc":@"123"}];
// [[NSNotificationCenter defaultCenter]postNotification:nofi];
//发送通知第二种方法:直接通知中心发送 object和userinfo都是通知的属性
[[NSNotificationCenter
defaultCenter]postNotificationName:@"testnoti"
object:@"1"
userInfo:@{@"zyc":@"123"}];
}
//第二个viewcontriller
@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(aa:)
name:@"testnoti"
object:nil];
}
//执行操作
- (void)aa:(NSNotification *)sender{
NSLog(@"%@",sender);//可以利用通知属性 object userinfo 传值过来
}
- (IBAction)pushto:(id)sender {
testViewController *testvc = [[testViewController
alloc]init];
[self.navigationController
pushViewController:testvc animated:YES];
}
相关文章推荐
- 深入理解javascript原型和闭包(8)——简述【执行上下文】上
- 深入理解javascript原型和闭包(7)——原型的灵活性
- window.load和doucment.ready有啥区别
- 计数排序
- CNKI PDF 全文下载用户脚本
- monkey环境搭建
- 二叉搜索树JAVA实现
- NodeJS学习系列课程笔记(NodeJs Stream 的相关使用方法)
- 深入理解javascript原型和闭包(6)——继承
- java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
- Ubuntu版本号查询
- JS生成指定范围内的随机数
- win7 64位系统不支持curl解决办法
- IT--linux--排错--log
- python md5计算
- 大众点评cat接入记录
- asp.net SessionState之sqlserver模式
- 深入理解javascript原型和闭包(5)——instanceof
- 深入理解javascript原型和闭包(4)——隐式原型
- elasticsearch进行parent/child类型聚合的测试