iOS打开和关闭键盘的通知使用

来源:互联网 发布:海岛奇兵战舰数据 编辑:程序博客网 时间:2024/06/03 22:45

1.注册键盘通知

//注册键盘出现通知;

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];

    //注册键盘消失通知;

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidHide:)name:UIKeyboardDidHideNotificationobject:nil];

    


2.键盘出现和消失时候方法实现


-(void)keyboardDidShow:(NSNotification *)notification

{

//键盘打开;

}

-(void)keyboardDidHide:(NSNotification *)notification

{

//键盘关闭

}


3.视图将要消失的时候删除键盘通知;


-(void)viewWillDisappear:(BOOL)animated

{

    //解除键出现盘通知;

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];

    //解除键盘隐藏通知;

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];

}







0 0
原创粉丝点击