免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1673 | 回复: 1
打印 上一主题 下一主题

NSNotificationCenter的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-01 22:00 |只看该作者 |倒序浏览




NSNotificationCenter的使用







From: http://blog.163.com/fuxiaohui@12 ... 826201196104455203/





什么是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)notificationWithNameNSString *)aName objectid)anObject;

+ (id)notificationWithNameNSString *)aName objectid)anObject userInfoNSDictionary *)aUserInfo;







NSNotificationCenter类

+ (id)defaultCenter;返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]

   

- (void)addObserverid)observer selectorSEL)aSelector nameNSString *)aName objectid)anObject;

如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer

. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer




- (void)postNotificationNSNotification *)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;




NSNotificationCenter 的使用方法举例(虚拟键盘的显示隐藏)



1. 定义一个方法

update

2.订阅通知

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(update) name"update" object:nil]



3. 在要发出通知消息的地方



[[NSNotificationCenter defaultCenter] postNotificationName"update" object:nil];

----------------------------

虚拟键盘显示和消失的通知

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(keyboardWasShown name:UIKeyboardDidShowNotification

object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(keyboardWasHidden name:UIKeyboardDidHideNotification

object:nil];




- (void)keyboardWasShown:(NSNotification *) aNotification{

if(keyboardShown)return;

NSDictionary *info = [aNotification userInfo];//获取通知信息

//get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Resize the scroll view

CGRect viewFrame = [scrollView frame];

viewFrame.size.height -= keyboardSize.height;

//Scroll the active text field into view

CGRect textFieldRect = [activeField frame];

[scrollView scrollRectToVisible:textFieldRect animated:YES];

keyboardShown = YES;

}

//Called when the UIKeyboardDidHideNotification is sent

- (void)keyboardWasHidden:(NSNotification *) aNotification{

NSDictionary *info = [aNotification userInfo];

//Get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Reset the height of the scroll view to its original value

CGRect viewFrame = [scrollView Frame];

viewFrame.size.height += keyboardSize.height;

scrollView.frame = viewFrame;

keyboardShown = NO;

}


论坛徽章:
0
2 [报告]
发表于 2012-03-01 22:20 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP