Add 'Done' and 'New line' buttons on keyboard in iPhone application
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.开发者_如何学Python
Please post your answer if anybody has some idea how to do it.
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.
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
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
}
精彩评论