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.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
Ajit
  • 101
  • 1
  • 2
  • 8
  • You can add these two buttons on a tool bar and by animation put that on top of keyboard. – Ishu Feb 02 '11 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 Feb 02 '11 at 11:11

3 Answers3

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.

Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
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
}
freezing_
  • 984
  • 1
  • 9
  • 13
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

vdaubry
  • 11,369
  • 7
  • 54
  • 76