ios 的通知机制

[[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 的通知机制

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容