ios 的通知机制

2018-10-26  本文已影响23人  虫yu
[[NSNotificationCenter defaultCenter] postNotificationName:@"IntroductionNotification"
                                                             object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setChapin) name:@"IntroductionNotification" object:nil];
//注销通知
- (void)dealloc {
    [super dealloc]; // 非ARC中需要调用此句
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"IntroductionNotification" object:nil];
//  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态

    键盘状态改变的时候,系统会发出一些特定的通知

    UIKeyboardWillShowNotification // 键盘即将显示

    UIKeyboardDidShowNotification // 键盘显示完毕

    UIKeyboardWillHideNotification // 键盘即将隐藏

    UIKeyboardDidHideNotification // 键盘隐藏完毕

    UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变

    UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕

    

    系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:

    UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame

    UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)

    UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间

    UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)

由于键盘的通知是系统自动的,因此可以不用创建通知,只需要注册一个监听器监听即可,当收到相应的通知去执行方法。

参考资料:

ios 的通知机制

上一篇 下一篇

猜你喜欢

热点阅读