iOS_NSNotificationCenter(通知中心简单建立)
2015-07-22 19:00
531 查看
通知中心 (先注册观察者,后发送通知)
接收通知中心发送的通知
移除通知中心的观察者(首选pop)
发送通知
- (IBAction)buttonDidClicked:(UIButton *)sender { SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES]; // 通知中心 *注册* 观察者 // 监听 123 频道消息 // 主要作用不是传值,而是实现相隔较远的页面之间进行交互 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:@"123" object:nil]; }
接收通知中心发送的通知
#pragma mark - 接收通知中心发送的通知 - (void)didReceiveNotification:(NSNotification *)sender { self.firstLabel.text = sender.userInfo[@"text"]; NSLog(@"welcome back!"); }
移除通知中心的观察者(首选pop)
#pragma mark - 移除通知中心的观察者(首选pop) // 如果内存控制好的话,也可以在dealloc里面写,ARC下也可以写dealloc - (void)dealloc { // 移除所有的观察者 [[NSNotificationCenter defaultCenter] removeObserver:self]; // 移除指定的观察者 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:nil]; }
发送通知
- (IBAction)backButtonDidClicked:(UIButton *)sender { // 发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:nil userInfo:@{@"text":self.textField.text}]; [self.navigationController popViewControllerAnimated:YES]; }
相关文章推荐
- 小胖说事33-----iOS给Lable字体添加行间距
- swfit中文学习
- 【直播预告】7月26日iOS免费公开课答疑第三期,有奖问答!
- iOS 图片相册调用封装
- iOS 敏捷开发,常用的宏
- iOS 倒计时NSTimer
- iOS —— 崩溃闪退日志
- ios初步修饰符简介
- iOS可持续化集成: Jenkins + bundler + cocoapods + shenzhen + fastlane + pgyer
- IOS开发多线程GCD的学习(二)
- iOS开发之多线程
- iOS—修改AFNetworking源文件可接收text/plain的方法
- iOS开发之蓝牙4.0 BLE开发
- iOS两个强制旋转屏幕的方法
- 【iOS开发系列】用简单工厂模式理解OC反射机制
- ios MVC
- ios 自定义cell中有textField,点击textField时,防止键盘挡住textField
- 构建 iOS 风格移动 Web 应用程序的8款开发框架
- 谈谈iOS7及以上版本navigationBar带来的坑
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)