十年专注于品牌网站建设 十余年专注于网站建设_小程序开发_APP开发,低调、敢创新、有情怀!
南昌百恒网络微信公众号 扫一扫关注
小程序
tel-icon全国服务热线:400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络微信小程序

百恒网络

南昌百恒网络

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

百恒网络 2017-09-30 5535

从事南昌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开发、南昌做网站、微信开发等方面的服务,如有需要,百恒随时为您效劳!


400-680-9298,0791-88117053
扫一扫关注百恒网络微信公众号
扫一扫打开百恒网络小程序

欢迎您的光顾,我们将竭诚为您服务×

售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售前咨询 售前咨询
 
售后服务 售后服务
 
售后服务 售后服务
 
备案专线 备案专线
 
×