百恒网络
关闭和打开键盘的通知使用方法

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

2017-09-30 6449

从事南昌APP开发的技术人员都知道,在关闭和打开键盘时,iOS系统分别会发出如下广播通知:UIKeyboardDidHideNotification和UIKeyboardDid- ShowNotification。使用广播通知的时候,百恒网络认为大家需要注意的是要在合适的时机注册和解除通知,而ViewController中的有关代码如下:

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

//注册键盘出现通知

NSNotificationCenter.defaultCenter().addObserver(self, selector:

"keyboardDidShow:", name: UIKeyboardDidShowNotification,object: nil)

//注册键盘隐藏通知

NSNotificationCenter.defaultCenter().addObserver(self, selector:

"keyboardDidHide:", name: UIKeyboardDidHideNotification,object: nil)

}

override func viewWillDisappear(animated: Bool) {

super.viewWillDisappear(animated)

//解除键盘出现通知

NSNotificationCenter.defaultCenter().removeObserver(self, name:UIKeyboardDidShowNotification, object: nil)

//解除键盘隐藏通知

NSNotificationCenter.defaultCenter().removeObserver(self, name:UIKeyboardDidHideNotification, object: nil)

}

func keyboardDidShow(notification: NSNotification) {

NSLog("键盘打开")

}

func keyboardDidHide(notification: NSNotification) {

NSLog("键盘关闭")

}

-(void) viewWillAppear:(BOOL)animated {

//注册键盘出现通知

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector (keyboardDidShow:)

name: UIKeyboardDidShowNotification object:nil];

//注册键盘隐藏通知

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector (keyboardDidHide:)

name: UIKeyboardDidHideNotification object:nil];

[super viewWillAppear:animated];

}

-(void) viewWillDisappear:(BOOL)animated {

//解除键盘出现通知

[[NSNotificationCenter defaultCenter] removeObserver:self

name: UIKeyboardDidShowNotification object:nil];

//解除键盘隐藏通知

[[NSNotificationCenter defaultCenter] removeObserver:self

name: UIKeyboardDidHideNotification object:nil];

[super viewWillDisappear:animated];

}

-(void) keyboardDidShow: (NSNotification *)notif {

NSLog(@"键盘打开");

}

-(void) keyboardDidHide: (NSNotification *)notif {

NSLog(@"键盘关闭");

}

南昌APP制作开发公司-百恒网络在此还要跟大家说明的是,注册通知在viewWillAppear:方法中进行,解除通知在viewWillDisappear:方法中进行。keyboardDidShow:消息是在键盘打开时发出的,keyboardDidHide:消息是在键盘关闭时发出的。

看完以上介绍后,大家是否对关于IOS中关闭和打开键盘的通知使用方法有所了解了呢? 如果还有哪些不太明白的地方,可随时来电和我们联系,我们专业为您讲解。此外,本公司专注于APP开发、南昌做网站、微信开发等方面的服务,如有需要,百恒随时为您效劳!


展开分享
服务
案例
动态
联系
咨询