简体   繁体   English

如何以编程方式为特定 UITextField 设置返回键?

[英]How do I programmatically set the Return Key for a particular UITextField?

I can't believe that this question hasn't been asked yet, but I have been unable to find it so that is my assumption.我不敢相信这个问题还没有被问到,但我一直找不到它,所以这是我的假设。

I have created custom UITableViewCel l subclasses using Interface Builder.我使用 Interface Builder 创建了自定义UITableViewCel l 子类。 These each contain a UILabel and a UITextField and are used for our login screen.它们每个都包含一个UILabel和一个UITextField并用于我们的登录屏幕。

On the first UITextField , I want the Keyboard Key to say Next .在第一个UITextField ,我希望键盘键说Next However, on the second UITextField , I want it to say "Done."但是,在第二个UITextField上,我希望它说“完成”。

How do I programmatically change the value of the Return Key on the keyboard?如何以编程方式更改键盘上Return Key的值? I cannot use Interface Builder , since that would make both keys appear as Done .我不能使用Interface Builder ,因为这会使两个键都显示为Done

Thanks!谢谢!

For the first UITextField , Read THIS tutorial on how to create a button on a Keyboard.对于第一个UITextField ,请阅读有关如何在键盘上创建按钮的教程 You can learn the logic from there and use a Next button instead of Done button.您可以从那里学习逻辑并使用Next按钮而不是Done按钮。

   //if you just want to make it appear NEXT//

    textField.returnKeyType = UIReturnKeyNext;

And for the second UITextField , use...对于第二个UITextField ,使用...

   // for DONE //

    textField.returnKeyType = UIReturnKeyDone;

You can in general create and rename the button to anything, and make sure you give the right IBAction on the event that that button is pressed.通常,您可以创建按钮并将其重命名为任何内容,并确保在按下该按钮的事件中提供正确的IBAction In case you do create your own custom button, look at the tutorial above to learn about placing it in the right coordinates and stuff.如果您确实创建了自己的自定义按钮,请查看上面的教程以了解如何将其放置在正确的坐标和内容中。

It can also be done by setting property in.xib file.也可以通过在.xib 文件中设置属性来完成。 You just have to set Return Key property of UITextField to Next under Attribute Inspector .您只需在Attribute Inspector下将UITextFieldReturn Key属性设置为Next

you have to invoke [textView reloadInputViews];你必须调用[textView reloadInputViews]; after setting the return key type to update the keyboard return key appearance immediately.设置返回键类型后立即更新键盘返回键外观。

DO IT AS FLLOW:按照流程进行:

textView.returnKeyType = UIReturnKeyDone; [textView reloadInputViews];

In swift 4, the following code can be used for replacing the "Return button" on keyboard with "search button" or "done button":在 swift 4 中,可以使用以下代码将键盘上的“返回按钮”替换为“搜索按钮”或“完成按钮”:

yourTextField.returnKeyType = UIReturnKeyType.search
yourTextField.returnKeyType = UIReturnKeyType.done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM