如何自动加载键盘(iOS SDK)?

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

How do I automatically load the keyboard (iOS SDK)?

iossdkviewkeyboarduitextfield

提问by pkluz

really simple question:

很简单的问题:

I got a view that consists of two textfields. When the view loads I want the keyboard to automatically pop up and focus the first field.

我得到了一个包含两个文本字段的视图。当视图加载时,我希望键盘自动弹出并聚焦第一个字段。

How do I do that? (In code? In IB?)

我怎么做?(在代码中?在 IB 中?)

Thanks a lot! wasabi

非常感谢!芥末

回答by Mark Granoff

In your viewDidAppear:method call [yourTextField becomeFirstResponder].

在您的viewDidAppear:方法中调用[yourTextField becomeFirstResponder].

回答by Devraj

[myTextField becomeFirstResponder]should do the trick.

[myTextField becomeFirstResponder]应该做的伎俩。

Also make sure you implement the UITextFieldDelegatemethods where you can hide the first responder.

还要确保您实现了UITextFieldDelegate可以隐藏第一响应者的方法。

回答by N V

As a clarification to MarkGranoff's answer;

作为对 MarkGranoff 回答的澄清;

If you put the [yourTextField becomeFirstResponder]inside viewDidLoador viewWillAppear- it will show the keyboard without waiting for a second or two (which happens if you put the becomeFirstRespondercall inside the viewDidAppear method).

如果你把[yourTextField becomeFirstResponder]里面viewDidLoador viewWillAppear- 它会在不等待一两秒钟的情况下显示键盘(如果你把becomeFirstResponder调用放在 viewDidAppear 方法中,就会发生这种情况)。

回答by limon

Well, i know this question has already been answered many times. It takes time to display a keyboard after viewDidAppearmethod gets called.

嗯,我知道这个问题已经回答了很多次了。viewDidAppear调用方法后显示键盘需要时间。

The reason that viewDidAppearis the last method which gets called in view life cycle after it disappears of course. Also, keyboard needs to be created as well, if it couldn't be found in memory. So, the first time it takes more time as i expected. These reasons cause a little delay that was bugging me.

原因当然viewDidAppear是它消失后在视图生命周期中被调用的最后一个方法。此外,如果在内存中找不到键盘,也需要创建键盘。所以,第一次像我预期的那样需要更多的时间。这些原因导致了一些困扰我的延迟。

I always call becomeFirstRespondermethod in viewWillAppearmethod to decrease displaying time for the keyboard, but i needed to display keyboard just after viewDidAppearmethod to not lose smooth animation.

我总是becomeFirstResponderviewWillAppear方法中调用方法来减少键盘的显示时间,但我需要在viewDidAppear方法之后显示键盘以免失去流畅的动画。

So, this little trick i came up with works like a charm. I tested it both on simulator and devices. If it doesn't work properly in your case, you have no other option but make it work as explained in accepted answer.

所以,我想出的这个小技巧就像一个魅力。我在模拟器和设备上都对其进行了测试。如果它在您的情况下不能正常工作,您别无选择,只能按照已接受的答案中的说明使其工作。

Well, what i'm doing is to create a UITextField(you can use any view that interacts with keyboard) and call become and resign first responder methods to put my keyboard in memory, if it isn't created yet. So, the second time, you're going to get keyboard in cache which accelerate keyboard displaying time. Well, i put these code the previous view controller that i'm going to display keyboard after.

好吧,我正在做的是创建一个UITextField(您可以使用任何与键盘交互的视图)并调用 become 和 resign first responder 方法来将我的键盘放入内存中,如果它还没有创建的话。因此,第二次,您将在缓存中获取键盘,从而加快键盘显示时间。好吧,我将这些代码放在我将要在之后显示键盘的前一个视图控制器中。

Here is the code :

这是代码:

- (void)viewDidAppear:(BOOL)animated
{
    UITextField *textField = [[UITextField alloc]init];
    [self.view addSubview:textField];
    [textField becomeFirstResponder];
    [textField resignFirstResponder];
    [textField removeFromSuperview];
}