ios 如何以编程方式为特定 UITextField 设置返回键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6311015/
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
How do I programmatically set the Return Key for a particular UITextField?
提问by TigerTrussell
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. These each contain a UILabel
and a UITextField
and are used for our login screen.
我UITableViewCel
使用 Interface Builder创建了自定义l 子类。这些每个都包含一个UILabel
和一个UITextField
,用于我们的登录屏幕。
On the first UITextField
, I want the Keyboard Key to say Next
. However, on the second UITextField
, I want it to say "Done."
首先UITextField
,我想让键盘键说Next
。但是,在第二个UITextField
,我希望它说“完成”。
How do I programmatically change the value of the Return Key
on the keyboard? I cannot use Interface Builder
, since that would make both keys appear as Done
.
如何以编程方式更改Return Key
键盘上的值?我不能使用Interface Builder
,因为这会使两个键都显示为Done
.
Thanks!
谢谢!
回答by Legolas
For the first UITextField
, Read THIS tutorial on how to create a button on a Keyboard. You can learn the logic from there and use a Next
button instead of Done
button.
首先UITextField
,阅读本教程,了解如何在键盘上创建按钮。您可以从那里学习逻辑并使用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. 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.
通常,您可以创建按钮并将其重命名为任何内容,并确保IBAction
在按下该按钮时赋予权限。如果您确实创建了自己的自定义按钮,请查看上面的教程以了解如何将其放置在正确的坐标中。
回答by Quanhua Guan
you have to invoke [textView reloadInputViews];
after setting the return key type to update the keyboard return key appearance immediately.
您必须[textView reloadInputViews];
在设置返回键类型后调用才能立即更新键盘返回键外观。
DO IT AS FLLOW:
顺其自然:
textView.returnKeyType = UIReturnKeyDone;
[textView reloadInputViews];
textView.returnKeyType = UIReturnKeyDone;
[textView reloadInputViews];
回答by vaishali
It can also be done by setting property in .xib file. You just have to set Return Key
property of UITextField
to Next
under Attribute Inspector
.
也可以通过在 .xib 文件中设置属性来完成。你只需要将Return Key
属性设置UITextField
为Next
under Attribute Inspector
。
回答by Mahyar
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