您的位置:首页 > 移动开发 > IOS开发

iOS NSNotificationCenter 通知

2016-02-19 10:45 411 查看
页面出现的时候注册通知,页面消失时移除通知。
一定要 成双成对 出现,如果你只在viewWillAppear
中 addObserver没有在viewWillDisappear 中 removeObserver那么当消息发生的时候,你的方法会被调用多次。
如下 :

-(void)viewWillAppear:(BOOL)animated

{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) 

name:UIKeyboardWillShowNotification object:nil];

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: