如下进行。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[view addSubView:nameTextField];
[nameTextField becomeFirstResponder];`
但键盘没有出现。
按Home键后,如果返回应用程序,则会出现键盘。
我不知道原因。
答案 0 :(得分:0)
使用以下代码检查:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillAppear(notification: NSNotification){
// Do something here
}
func keyboardWillDisappear(notification: NSNotification){
// Do something here
}
并且不要忘记在viewwillappear中删除观察者,如下所示:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}