UIView和NSNotificationCenter

UIView绑定了广播监听,绑定的位置和移除绑定的位置应该放在什么地方。

- (void)willMoveToWindow:(UIWindow *)newWindow{
    [super willMoveToWindow:newWindow];
    if (self.window != nil) {
        [[NSNotificationCenter defaultCenter]removeObserver:self];
    }
}
- (void)didMoveToWindow {

    if (self.window != nil) {
        //添加键盘显示与消失通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoWillAppear:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoWillDismiss:) name:UIKeyboardWillHideNotification object:nil];
    }
}

你可能感兴趣的:(UIView和NSNotificationCenter)