iOS NSNotificationCenter 通知
2016-02-19 10:45
411 查看
页面出现的时候注册通知,页面消失时移除通知。
一定要 成双成对 出现,如果你只在viewWillAppear
中 addObserver没有在viewWillDisappear 中 removeObserver那么当消息发生的时候,你的方法会被调用多次。
如下 :
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
一定要 成双成对 出现,如果你只在viewWillAppear
中 addObserver没有在viewWillDisappear 中 removeObserver那么当消息发生的时候,你的方法会被调用多次。
如下 :
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
相关文章推荐
- iOS开发之使用XMPPFramework实现即时通信(三)
- iOS开发之使用XMPPFramework实现即时通信(二)
- iOS开发之使用XMPPFramework实现即时通信(一)
- iOS创建多个子工程的方法
- 此证书的签发者无效Missing iOS Distribution signing identity问题解决
- IOS之抖动效果
- iOS开发之定位CLLocationManager
- iOS分类(Category)和类扩展(Extension)的简单区别
- iOS开发ARC内存管理技术要点
- iOS开发证书失效--Missing iOS Distribution signing identity
- IOS设置菜单界面的设计思路
- iOS开发如何去掉某种类型的警告
- ios nil、NULL和NSNull 的使用
- 设置iOS的Code Snippet 快捷代码块
- 可变字典(NSMutableDictionary)简介
- iOS让软键盘消失的简单方法
- iOS让软键盘消失的简单方法
- iOS让软键盘消失的简单方法
- iOS:iOS开发中用户密码保存位置
- iOS开发中的内存管理相关