Search code examples
iphoneiosuitextfieldfirst-responder

First Responder not setting focus on uitextfield


I am trying to set the focus of a uitextfield. Using this code

[_partNoTextField becomeFirstResponder];

When I run the code, the keyboard goes away and the text field is not in focus. I need it to focus on _partNoTextField after it runs my IBAction checkpartno. On the textfield I have setup Did end on exit to call my checkpartno ibaction so when a user hits return it runs it. How can I get the focus to go back to the _partNoTextField ?


Solution

  • Don't use the "did end on exit" selector but instead set a delegate from the text field back to your controller and then:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        // i.e. run your IBAction when clicking on return key
        //
        // here I am assuming your checkparno action lives in the 
        // same object as your delegate
        [self checkpartno: textField]; 
        return NO; // returning NO means the keyboard stays up
    }