ios 检测 UITextField 的焦点变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10671698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Focus Change for UITextField
提问by Dennis
I'm trying to set the animation for the view to move up when keyboard is hiding and appearing for the text fields and I got it to work perfectly fine, but when the focus moves from one text field to another, it doesn't work since the keyboard was already shown.
我正在尝试设置视图的动画以在键盘隐藏和出现文本字段时向上移动,并且我让它工作得非常好,但是当焦点从一个文本字段移动到另一个文本字段时,它不起作用因为键盘已经显示了。
In viewDidLoad, I registered the following:
在 viewDidLoad 中,我注册了以下内容:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and then in the keyboardWillShow and keyboardWillHide methods, it determines if the view should move or not and animate accordingly. But if a keyboard was already shown and the user clicks on another text field that needs the view to move up, the method wouldn't get called. Is there any way to detect if a focus has been changed to another text field when the keyboard was already shown? It would be great if there is a way to do this without having to set all the text fields to delegates.
然后在 keyboardWillShow 和 keyboardWillHide 方法中,它确定视图是否应该移动并相应地设置动画。但是如果已经显示了键盘并且用户单击了另一个需要向上移动视图的文本字段,则不会调用该方法。当键盘已经显示时,有什么方法可以检测焦点是否已更改为另一个文本字段?如果有一种方法可以做到这一点而不必将所有文本字段设置为委托,那就太好了。
Thanks in advance.
提前致谢。
回答by Malek_Jundi
Use the UITextField
delegate methods .. its better on your case than the keyboard methods .. when textField got focus the - (void)textFieldDidBeginEditing:(UITextField *)textField;
will be fired .. and when it lost focus - (void)textFieldDidEndEditing:(UITextField *)textField;
will be fired.
使用UITextField
委托方法 .. 在你的情况下比键盘方法更好 .. 当 textField 获得焦点时,- (void)textFieldDidBeginEditing:(UITextField *)textField;
将被触发 .. 当它失去焦点时- (void)textFieldDidEndEditing:(UITextField *)textField;
将被触发。
回答by Warif Akhand Rishi
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
if (textField.tag == 1) { //first textField tag
//textField 1
}
else {
//textField 2
}
}
回答by Raymond Led Carasco
Use UITextFieldDelegate
使用 UITextFieldDelegate
and
和
func textFieldDidBeginEditing(textField: UITextField) {
println("did")
if textField.tag == 1{
self.txtFullName.layer.borderColor = UIColor.blueColor().CGColor
}
}
回答by Raul Olmedo
In swift 4:
在快速 4:
- Implement
UITextFieldDelegate
into yourViewController
class - Add the Outlets for the
textFields
you want to detect focus on - Implement function
textFieldDidBeginEditing
(in case you want to be notified on that specific action, see Apple's documentation) and fill it with your code - On
viewDidLoad()
assign yourViewController
class as the delegate for thetextField
you previously added in step 2
- 实施
UITextFieldDelegate
到您的ViewController
班级中 - 添加
textFields
要检测焦点的 Outlets - 实现功能
textFieldDidBeginEditing
(如果您想收到有关该特定操作的通知,请参阅Apple 的文档)并用您的代码填充它 - 在
viewDidLoad()
分配你的ViewController
类作为代表为textField
您先前在步骤2中添加
It must look similar to:
它必须类似于:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var yourTextField: UITextField!
func textFieldDidBeginEditing(_ textField: UITextField) {
// Your code here
}
override func viewDidLoad() {
super.viewDidLoad()
yourTextField.delegate = self
}
...
}