NSNotificationCenter 通知的使用

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];
}

释放通知的这两种方法的利弊会在下一篇博客中记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值