Sergey Katranuk
Sergey Katranuk

Reputation: 1182

How to change the keyboard Done button behavior?

Alright, so my goal is that when the keyboard Done button is tapped after introducing something in a UITextField, it should trigger one of my actions but not dismiss the keyboard.

Right now, I've connected the UITextField with my action through the Did End on Exit event and each time I tap the Done button when I'm finished typing, the keyboard goes away.

Let me know if you need further clarification.

Upvotes: 6

Views: 6749

Answers (4)

pierre23
pierre23

Reputation: 3936

In Swift it would be :

//MARK: UITextFieldDelegate

func textFieldShouldReturn(textField: UITextField) -> Bool
{
    textField.resignFirstResponder()

    return true
}

Upvotes: 1

kaspartus
kaspartus

Reputation: 1365

You have to use the delegate method of UITextField. (Set delegate of TextField to correct viewController in Interface Builder or in source)

Then, go to .h file of textField's viewcontroller:

@interface correctViewController(type yours) :UIViewController <UITextFieldDelegate>
//now correctViewController "understands" our textfield.

Go to correctViewController.m file:

//Create delegate method of textField
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder]; //to hide keyboard
    return NO;
}

Looks like this.

Upvotes: 0

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

Take a look at UITextFieldDelegate Protocol Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

You can use this method to control the behavior of your keyboard:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    return NO;
}

Upvotes: 0

aopsfan
aopsfan

Reputation: 2441

Try setting textField.delegate = self in code, as well as conforming to the UITextFieldDelegate protocol. Then implement this method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Do something
}

Upvotes: 11

Related Questions