從事南昌APP開發(fā)的技術(shù)人員都知道,在關(guān)閉和打開鍵盤時(shí),iOS系統(tǒng)分別會(huì)發(fā)出如下廣播通知:UIKeyboardDidHideNotification和UIKeyboardDid- ShowNotification。使用廣播通知的時(shí)候,百恒網(wǎng)絡(luò)認(rèn)為大家需要注意的是要在合適的時(shí)機(jī)注冊和解除通知,而ViewController中的有關(guān)代碼如下:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//注冊鍵盤出現(xiàn)通知
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)
//解除鍵盤出現(xiàn)通知
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("鍵盤關(guān)閉")
}
-(void) viewWillAppear:(BOOL)animated {
//注冊鍵盤出現(xiàn)通知
[[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 {
//解除鍵盤出現(xiàn)通知
[[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(@"鍵盤關(guān)閉");
}
南昌APP制作開發(fā)公司-百恒網(wǎng)絡(luò)在此還要跟大家說明的是,注冊通知在viewWillAppear:方法中進(jìn)行,解除通知在viewWillDisappear:方法中進(jìn)行。keyboardDidShow:消息是在鍵盤打開時(shí)發(fā)出的,keyboardDidHide:消息是在鍵盤關(guān)閉時(shí)發(fā)出的。
看完以上介紹后,大家是否對關(guān)于IOS中關(guān)閉和打開鍵盤的通知使用方法有所了解了呢? 如果還有哪些不太明白的地方,可隨時(shí)來電和我們聯(lián)系,我們專業(yè)為您講解。此外,本公司專注于APP開發(fā)、南昌做網(wǎng)站、微信開發(fā)等方面的服務(wù),如有需要,百恒隨時(shí)為您效勞!