Objective-C中的发通知的(Notification)
2015-10-08 16:26
253 查看
第一个页面进来后,先注册监听
第二个页面进来后点击button 然后发消息
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //先在通知中心注册 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:NOTIFICATION_MESSAGE object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:YES]; // [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void)doSomething:(NSNotification *)notification { //接受消息 NSDictionary *Info = [notification userInfo]; //输出收到的信息 NSLog(@“消息收到:%@", Info[@"notification"]); }
第二个页面进来后点击button 然后发消息
@implementation ChatViewController - (void)viewDidLoad { [super viewDidLoad]; [self initContentView]; } - (void)initContentView { UIButton *_commitBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 179, kDEVICEWIDTH-30, 45)]; _commitBtn.backgroundColor = [UIColor grayColor]; [_commitBtn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside]; [_commitBtn setTitle:@"发消息" forState:UIControlStateNormal]; _commitBtn.titleLabel.textColor = [UIColor whiteColor]; _commitBtn.layer.cornerRadius = 4; [self.view addSubview:_commitBtn]; } - (void)commitClick { [self sendMessage]; } #pragma mark - NSNotification method -(void)sendMessage { //把要发送的信息放入字典中 NSDictionary *message = @{@"notification" : @“NSNotification消息测试通过!”}; //创建通知对象 NSNotification * notification = [NSNotification notificationWithName:NOTIFICATION_MESSAGE object:self userInfo:message]; //向通知中心发送消息(发布消息) //[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE object:nil]; //向通知中心发送消息(发布消息) [[NSNotificationCenter defaultCenter] postNotification:notification]; }
相关文章推荐
- OC基础15:内存管理和自动引用计数
- iOS开发系列--Objective-C之协议、代码块、分类
- OC基础14:使用文件
- 返回object问题,在window.open后
- /persistence.cpp:5008: error: (-2) The node does not represent a user object (unknown type?) in func
- iOS开发系列--Objective-C之类和对象
- synchronized(class)、synchronized(this)与synchronized(object)的区别分析
- iOS进阶:Objective-C runtime(一)
- java中Object类 源代码详解
- 初识 Objective - C
- Objective-C:禁止调用方法
- Object.wait()与Object.notify()的用法
- 在Object-C中使用@property和@synthesize的区别
- java Object类学习
- Objective-C---9---内存管理
- Objective-C对象属性设置
- Lost in quantization: improving particular object retrieval in large scale image databases
- iOS开发之Objective-C与JavaScript的交互
- iOS开发之Objective-C与JavaScript的交互
- Objective-C Class Ivar Layout 探索