NSNotificationCenter 通知的使用
来源:互联网 发布:sql order by limit 编辑:程序博客网 时间:2024/06/01 07:24
NSNotificationCenter 通知的使用
之前在项目里使用了键盘的通知,但是没释放,导致之后的页面一直在调用键盘的事件,为了加深印象,我在此记录下来
//注册通知时请放在viewWillAppear里,不然等页面pop回到这个页面时,键盘的通知功能就没有了- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //使用NSNotificationCenter 键盘出现时 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown1:) name:UIKeyboardDidShowNotification object:nil]; //使用NSNotificationCenter 键盘隐藏时 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden1:) name:UIKeyboardWillHideNotification object:nil];}//实现当键盘出现的时候计算键盘的高度大小- (void)keyboardWasShown1:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; //kbSize键盘尺寸 (有width, height) CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; //输入框随键盘上升 self.toolBarView.frame = CGRectMake(0, self.scrollView.frame.size.height+self.scrollView.frame.origin.y - kbSize.height, SCREEN_WIDTH, 60); //收益框随键盘上升 self.showView.frame = CGRectMake(0, self.scrollView.frame.size.height+self.scrollView.frame.origin.y - kbSize.height - 40 , SCREEN_WIDTH, 40); self.showView.hidden = NO; // self.showLabel.text = [NSString stringWithFormat:@" 预期收益: -- 元 可用余额: --元"]; // self.showLabel.attributedText =[Common setupAttributeString:self.showLabel.text highlightText:@"预期收益:" Color:RGB(255,159,15) highlightText1:@"元" Color1:RGB(255,159,15) highlightText2:@"可用余额:" Color2:RGB(255, 159, 15) highlightText3:@"元" Color3:RGB(255, 159, 15)]; if (self.inTextField.text.length != 0) { self.showView.hidden = NO; } DLOG(@"获取余额接口请求"); [self CBbalance];//获取余额}//当键盘隐藏的时候- (void)keyboardWillBeHidden1:(NSNotification*)aNotification{ //do something self.toolBarView.frame = CGRectMake(0, self.scrollView.frame.size.height+self.scrollView.frame.origin.y , SCREEN_WIDTH, 60); self.showView.frame = CGRectMake(0, self.scrollView.frame.size.height+self.scrollView.frame.origin.y - 40 , SCREEN_WIDTH, 40); if (self.inTextField.text.length == 0) { self.showView.hidden = YES; }else{ self.showView.hidden = NO; }}
我这个需求是在键盘出现时请求一个接口,并显示一行文字,提醒用户.当时没有释放,在后面的界面用到键盘时,接口还是会请求,只是文字的Label初始化不在,所以没有显示文字,导致我当时也没发现这个问题.到后来用户请求接口比较慢的时候就出现问题了,键盘上不来.
-(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:YES]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}
只要在页面消失的时候,释放通知就可以了
下面说一下正常的NSNotificationCenter 的注册和释放
//在当前页面注册个name通知,并在通知被调用后执行requestPhoneContrast方法[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestPhoneContrast) name:@"name" object:nil];//在已经注册过通知之后的任何页面调用通知,注意name不要写错了 [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil];//在当前页面释放通知-(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:YES]; [[NSNotificationCenter defaultCenter] removeObserver:self name:name object:nil];}//或- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];}
释放通知的这两种方法的利弊会在下一篇博客中记录
阅读全文
1 0
- NSNotificationCenter 通知的使用
- NSNotificationCenter 通知的使用
- 通知中心NSNotificationCenter的使用
- iOS通知NSNotificationCenter的使用
- ios NSNotificationCenter通知的使用
- 使用通知中心NSNotificationCenter遇到的问题
- Swift里通知的使用NSNotificationCenter
- IOS中通知中心(NSNotificationCenter)的使用
- IOS中通知中心(NSNotificationCenter)的使用
- iOS--通知的使用(NSNotificationCenter )
- iOS中通知的使用NSNotificationCenter
- iOS 通知中心(NSNotificationCenter)的使用
- iOS中通知的使用NSNotificationCenter
- swift中通知NSNotificationCenter的使用
- iOS开发:NSNotificationCenter,通知中心的使用
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- hadoop2.6.0 nodemanager启动后过一段时间就自动关闭
- python爬虫爬取内容中,\xa0,\u3000的含义
- 使用setTimeout制作倒计时
- Java中的Main线程
- Error:Execution failed for task ':UpMarqueeView:mergeReleaseResources'.
- NSNotificationCenter 通知的使用
- 微信小程序开发(2)--之简单介绍数据绑定及渲染
- Python 代码风格
- Android自动化测试|UI Automatorviewer
- 邮箱和纯数字的判断
- 数据库备份脚本
- Https和网络抓包工具的一些疑问
- windows 句柄和句柄表
- LeetCode#1 Two Sum