ios之Notification
来源:互联网 发布:如何用c语言开发软件 编辑:程序博客网 时间:2024/06/01 18:23
什么是Notification?这个要求其实也很容易实现. 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏. 对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做 observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). center将该notifications转发给所有注册对该notification感兴趣的对象. 我们把这些发送notification的对象叫做poster
很多的标准Cocoa类会发送notifications: 在改变size的时候,Window会发送notification; 选择table view中的一行时,table view会发送notification;我们可以在在线帮助文档中查看到标准cocoa对象发送的notification
在对象释放前,我们必须从notification center移除我们注册的observer. 一般我们在dealloc方法中做这件事
NSNotification类
提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和 object.
- (NSString *)name;
- (id)object;
- (NSDictionary *)userInfo;我们想要notification对象传递更多的信息
+ (id)notificationWithName:(NSString *)aName object:(id)anObject;
+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;
NSNotificationCenter类
+ (id)defaultCenter;返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject
接下来给大家看一下例子。
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.05 green:0.6 blue:0.3 alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.5 alpha:1];
count = 0;
timer = [NSTimer scheduledTimerWithTimeIn
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@”Note” object:nil];
}
-(void)updateTimer:(NSTimer*)time{
count++;
self.title = [NSString stringWithFormat:@"%d",count];
if (count%5 == 0) {
[[NSNotificationCenter defaultCenter] postNotificationName:@”Note” object:nil];
}
}
-(void)receiveNotification:(NSNotification*)note{
UIAlertView* noteView = [[UIAlertView alloc] initWithTitle:nil message:@”You receive a notification!!” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[noteView show];
[noteView release];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObject:self];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
if (timer != nil) {
[timer release];
timer = nil;
}
}
- ios之Notification
- ios开发之Notification
- iOS之Notification
- iOS技巧之Notification,BadgeView
- iOS技巧之Notification,BadgeView
- iOS之Local Notification(本土通知)
- iOS学习之——Notification补充
- iOS开发之Notification与多线程
- iOS Notification
- iOS Notification
- ios notification
- iOS--Notification
- ios notification
- iOS推送之本地推送(iOS Notification Of Local Notification)
- iOS推送之远程推送(iOS Notification Of Remote Notification)
- iOS推送之远程推送(iOS Notification Of Remote Notification)
- Android倚天剑之Notification之亮剑IOS
- ios开发之何时用delegate,何时用notification?
- 网络编程 UDP 数据传输
- 黑马程序员--异常
- 母函数详解
- 配置python问题实践中的解决方法集锦(本问题集锦将持续更新,欢迎提出问题并在文下交流)
- eclipse CDT禁止每次调试新工程都停在main函数的方法
- ios之Notification
- Linux下获取存储介质扇区大小
- 幸福能否递归
- HDU1026 Ignatius and the Princess I
- OC学习那点事:构造方法和description方法
- easymock源码学习笔记(3)--录制
- 25个CSS3 渐变和动画效果教程
- 系统安装の网刻
- Samba共享问题