4

I have created a window based application with a UITabbarController as the RootViewController. In one of the tabs, i have provided UITextField and UITextView. I want to provide two buttons on the keyboard itself:

  • Done - which will hide the keyboard.
  • Enter - for new line.

Please post your answer if anybody has some idea how to do it.

2
  • You can add these two buttons on a tool bar and by animation put that on top of keyboard.
    – Ishu
    Commented Feb 2, 2011 at 11:10
  • Keyboard having return key for new line for textView and for textfield you will not need any new line as well.
    – Ishu
    Commented Feb 2, 2011 at 11:11

3 Answers 3

4

For the UITextField you can change the return key to a done key by setting the following:

targetTextField.returnKeyType = UIReturnKeyDone;

However, you won't be able to have a Enter and Done key at the same time without custom addition of views to the keyboard.

Also, to control the done behavior of the keyboard you have to implement a UITextFieldDelegate method:

targetTextField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
     return YES;  //dismisses the keyboard
}

I know you can set the returnKeyType for a UITextView but I'm not sure if you can manipulate the return key behavior.

1

You have a tutorial on how add subviews to the iPhone keyboard here :

http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7350-adding-subviews-custimize-keyboard.html

Hope this helps, Vincent

1

For some reason return YES; didn't work on its own. that worked for me :

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

    if (textField.returnKeyType == UIReturnKeyNext) {
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (nextResponder) {
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
        }
    }

    if (textField.returnKeyType == UIReturnKeyDone) {
        [textField resignFirstResponder];
    }
    return YES;  //dismisses the keyboard
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.