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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:27:24  来源:igfitidea点击:

Detect Focus Change for UITextField

iphoneiosios5uitextfieldxcode4.3

提问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 UITextFielddelegate 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:

  1. Implement UITextFieldDelegateinto your ViewControllerclass
  2. Add the Outlets for the textFieldsyou want to detect focus on
  3. Implement function textFieldDidBeginEditing(in case you want to be notified on that specific action, see Apple's documentation) and fill it with your code
  4. On viewDidLoad()assign your ViewControllerclass as the delegate for the textFieldyou previously added in step 2
  1. 实施UITextFieldDelegate到您的ViewController班级中
  2. 添加textFields要检测焦点的 Outlets
  3. 实现功能textFieldDidBeginEditing(如果您想收到有关该特定操作的通知,请参阅Apple 的文档)并用您的代码填充它
  4. 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
    }

    ...

}