Mason
Mason

Reputation: 7103

UITextFieldDelegate textFieldShouldReturn caller

Is there a way to detect whether -[UITextFieldDelegate textFieldShouldReturn] was called by a user hitting the "Done" key on the keyboard (or equivalent), or if it was called by a programmatic call to -[UITextField resignFirstResponder]?

Upvotes: 0

Views: 154

Answers (3)

Charan Giri
Charan Giri

Reputation: 1097

@interface ViewController ()<UITextFieldDelegate>

-(void) viewDidLoad
{
     textfieldObject.delegate = self;
}

-(BOOL)textFieldShouldReturn:( UITextField * )textField
{
     return YES;
}

Upvotes: 1

manujmv
manujmv

Reputation: 6445

Yes you can use the delegate methods to determine whether it is resignFirstResponder or return key pressed.

- (BOOL)textFieldShouldReturn:(UITextField *)textField  {

   // here return key pressed will be handled
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

   // used to handle resignFirstResponder

}

Upvotes: 1

hukir
hukir

Reputation: 1743

I haven't tested this specifically, but you could check the isFirstResponder status of the text field. If it is the first responder, you know that the enter key was pressed. Otherwise, you know resignFirstResponder was called. This assumes that resignFirstResponder changes the status before calling textFieldShouldReturn.

Upvotes: 0

Related Questions