我在视图中有六个UITextFields,我想决定视图是否必须移动。在移动视图之前,如何检查选择了哪个TextField?
这是我的代码显示键盘和移动视图:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height - 85
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y += keyboardSize.height - 85
}
}
答案 0 :(得分:3)
如果您想知道选择了哪个UITextField
,可以使用textFieldDidBeginEditing
和textFieldDidEndEditing
func textFieldDidBeginEditing(textField: UITextField!) {
currentSelectedTextField = textField
}
func textFieldDidEndEditing(textField: UITextField!) {
currentSelectedTextField = nil
}
有关如何在选择UITextField
:https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html