Vins
Vins

Reputation: 1934

UITextField delegate

I have a UITextFiled to which I applied the delegate to close the keyboard by pressing "done".

    ...
    textfield.returnKeyType = UIReturnKeyDone;
    textfield.delegate = self;
    ....

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

    [textField resignFirstResponder];
    return YES;
}

How do I invoke a method when I press "done"?

Upvotes: 2

Views: 17137

Answers (4)

Mahendra Vishwakarma
Mahendra Vishwakarma

Reputation: 494

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [add your method here];
    return YES;
}

Upvotes: 0

jlink
jlink

Reputation: 692

Is it that what you want ?

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [add your method here];
    return YES;
}

Upvotes: 8

waterforest
waterforest

Reputation: 31

#pragma mark -
#pragma mark Text Field Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField canResignFirstResponder]) {
        [textField resignFirstResponder];
    }

    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    // add your method here

    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

}

Upvotes: 2

sergio
sergio

Reputation: 69027

When the "done" button is pressed, your editing on the field will end. You can call the delegate method:

textFieldDidEndEditing:

This method is called when the field resigns its first responder status, so it will be called also when you execute your textFieldShouldReturn like you specified it.

Upvotes: 2

Related Questions