xcode 在 iPhone 应用程序的键盘上添加“完成”和“换行”按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4873525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:10:08  来源:igfitidea点击:

Add 'Done' and 'New line' buttons on keyboard in iPhone application

iphoneobjective-cxcodeprogrammatically-created

提问by Ajit

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

我创建了一个基于窗口的应用程序,UITabbarController其中RootViewController. 在其中一个选项卡中,我提供了UITextFieldUITextView。我想在键盘上提供两个按钮:

  • Done - which will hide the keyboard.
  • Enter - for new line.
  • 完成 - 这将隐藏键盘。
  • 输入 - 换行。

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

如果有人知道如何去做,请发布您的答案。

回答by Brody Robertson

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

对于 UITextField,您可以通过设置以下内容将返回键更改为完成键:

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.

但是,如果没有自定义添加到键盘的视图,您将无法同时使用 Enter 和 Done 键。

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

此外,要控制键盘的完成行为,您必须实现 UITextFieldDelegate 方法:

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.

我知道您可以为 UITextView 设置 returnKeyType 但我不确定您是否可以操纵返回键行为。

回答by freezing_

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

出于某种原因返回YES;没有单独工作。这对我有用:

- (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
}

回答by vdaubry

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

你有一个关于如何向 iPhone 键盘添加子视图的教程:

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

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

Hope this helps, Vincent

希望这会有所帮助,文森特