强制键盘通过按钮显示(iOS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2750445/
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-08-30 17:10:43  来源:igfitidea点击:

Force keyboard to show up via button (iOS)

iosiphoneuitextviewuikeyboard

提问by Mc.Lover

I have an UITextView and I don't want check editable option, how can call keyboard via a button? This code doesn't work for me!

我有一个 UITextView 并且我不想检查可编辑选项,如何通过按钮调用键盘?这段代码对我不起作用!

-(IBAction) yourButtonClick
{
    [myTextView becomeFirstResponder];
    [self.view addSubview:myTextView]; 
}

回答by Arrix

From the iPhone Application Programming Guide

来自 iPhone 应用程序编程指南

However, you can programmatically display the keyboard for an editable text view by calling that view's becomeFirstResponder method. Calling this method makes the target view the first responder and begins the editing process just as if the user had tapped on the view.

但是,您可以通过调用该视图的 becomeFirstResponder 方法以编程方式显示可编辑文本视图的键盘。调用此方法使目标视图成为第一响应者并开始编辑过程,就像用户点击视图一样。

So to show the keyboard programmatically,

所以要以编程方式显示键盘,

[textView becomeFirstResponder];

However, the keyboard will never show if the textView is not editable.

但是,如果 textView 不可编辑,键盘将永远不会显示。

The purpose of showing the keyboard is to allow editing. I assume you just don't want the keyboard to appear when the user taps the text view. In this case, you can enable editable programmatically when the button is tapped.

显示键盘的目的是允许编辑。我假设您只是不希望在用户点击文本视图时出现键盘。在这种情况下,您可以在点击按钮时以编程方式启用可编辑。

-(IBAction) yourButtonClick
{
     myText.editable = YES;
     [myText becomeFirstResponder];

}

Then in the UITextViewDelegate, disable editable when the user finishes editing.

然后在 UITextViewDelegate 中,当用户完成编辑时禁用可编辑。

- (void)textViewDidEndEditing:(UITextView *)textView {
  textView.editable = NO;
}