iPhone开发之NSNotificationCenter(通知)的使用方法
来源:互联网 发布:如何卸载软件 编辑:程序博客网 时间:2024/05/16 17:08
Phone软件开发的时候会遇到这种情况:打开APP后会在后台运行某个方法,例如下载文件,下载完成后可能需要调用某个方法来刷新界面,这时候可能没法在下载的函数中回调。NSNotificationCenter(通知)是一个很好的选择。
通知使用起来灰常的简单:
1、定义通知:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @"back" object: nil];
2、定义通知中使用的方法:
- (void)callBack{ NSLog(@"i am back."); }
3、调用通知:
- (void)getIT{ NSLog(@"get it."); //发出通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self]; }
在调用通知的时候程序会在整个项目中寻找此通知的名称,找到后发出请求,因此通知的名称需要在整个项目中唯一。
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad。 Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification,系统中(UIKeyboardDidShowNotification 等) 以及某些第三方组件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。
NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式:
1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的 Selector 或 Block
2. NSNotificationCenter 在某个时机激发事件(以字符串命名)
3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或 Block
- iPhone开发之NSNotificationCenter(通知)的使用方法
- iPhone开发之NSNotificationCenter(通知)的使用方法
- iPhone开发之NSNotificationCenter(通知)的使用方法
- iPhone开发之NSNotificationCenter(通知)的使用方法
- 【转载】iPhone开发之NSNotificationCenter(通知)的使用方法
- iOS开发之NSNotificationCenter(通知)的使用方法
- iOS开发之NSNotificationCenter(通知)的使用方法
- iOS开发之:NSNotificationCenter(通知)的使用方法
- NSNotificationCenter(通知)的使用方法
- iOS开发 NSNotificationCenter 通知的使用方法
- Objective-C NSNotificationCenter (通知)的使用方法
- IOS NSNotificationCenter 通知使用方法
- NSNotificationCenter 通知使用方法详解
- iOS开发之通知中心(NSNotificationCenter)
- iOS开发之通知中心(NSNotificationCenter)
- iOS开发之通知中心(NSNotificationCenter)«
- iOS开发之NSNotificationCenter(通知)使用介绍
- 【IPhone开发】NSNotificationCenter观察者模式给主线程发送通知
- iPhone真机调试日志读取 iPhone Crash Logs
- Android 取消Log输出
- container_of, 没那么简单
- 从RGB色转为灰度色算法
- (2009_ICCV)An HOG-LBP Human Detector with Partial Occlusion Handling
- iPhone开发之NSNotificationCenter(通知)的使用方法
- 关于STARTUP.A51和INIT.A51 参考资料
- 修复Chrome中Shockwave Flash has crashed问题
- Tab键
- OpenCV-2.4.3+VS2010+Win7(32位)安装与配置
- flex类绑定
- linux ls -l 命令 详解
- SP框架限制分页数字长度
- 组播程序