Detect Focus Change for UITextField
Asked Answered
Y

4

44

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:

[[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.

Thanks in advance.

Yuletide answered 20/5, 2012 at 7:47 Comment(0)
W
91

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.

Warplane answered 20/5, 2012 at 7:51 Comment(4)
Thanks for the response. If I do this, is there a way to get the userInfo? Because in those methods I got the UIKeyboardAnimationDurationUserInfoKey and UIKeyboardFrameEndUserInfoKey from the userInfo to set up the animation, but I'm not sure how I could do the same when there is no notification getting passed in as a parameter?Yuletide
UIKeyboardAnimationDurationUserInfoKey, for the keyboard animation duration time (so the animation for the view moving could match), and UIKeyboardFrameEndUserInfoKey to get the height of the keyboard. I didn't want to hard code the values.Yuletide
since the animation duration and keyboard height are static variables you can read it once and store it in NSUSerDefaults so you can use them anywhere later.Warplane
Yeah I should do that.. I was just feeling lazy and wanted to see if there is an easier way to get notified when the focus has been changed to another text field. Thanks anyway!Yuletide
R
10
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
if (textField.tag == 1) { //first textField tag
    //textField 1
}
else {
   //textField 2
}
}
Replacement answered 20/5, 2012 at 8:19 Comment(0)
N
9

Use UITextFieldDelegate

and

func textFieldDidBeginEditing(textField: UITextField) {
        println("did")
        if textField.tag == 1{
            self.txtFullName.layer.borderColor = UIColor.blueColor().CGColor
        }
    }
Nadda answered 23/9, 2015 at 5:19 Comment(1)
It's actually func textFieldDidBeginEditing(_ textField: UITextField) { the function signatureDejesus
E
6

In swift 4:

  1. Implement UITextFieldDelegate into your ViewController class
  2. Add the Outlets for the textFields you 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 ViewController class as the delegate for the textField you previously added in step 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
    }

    ...

}
Eugenol answered 18/9, 2018 at 20:39 Comment(2)
verification passedWittgenstein
it should have a flag for that, most of times you can have several ones and want to check if one is focused or not.Annabelannabela

© 2022 - 2024 — McMap. All rights reserved.