我可以动态更改 UITextField 的属性 UIReturnKeyType 吗?

发布于 2024-11-06 07:50:30 字数 252 浏览 1 评论 0原文

我们可以将 UITextField 的这个属性设置为“Send”、“Search”、“Go”等:

@property(nonatomic) UIReturnKeyType returnKeyType

但是在使用 UITextField 时设置它根本不会改变。

我想要这种行为: 当UITextField为空时,让returnKeyType为“Return”,否则让returnKeyType为“Send”。

we can set this property of UITextField to "Send", "Search", "Go", etc.:

@property(nonatomic) UIReturnKeyType returnKeyType

But setting it while using the UITextField doesn't change at all.

I want this behavior:
When UITextField is empty, let returnKeyType be "Return", otherwise let returnKeyType be "Send".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笨笨の傻瓜 2024-11-13 07:50:30

通常,在显示键盘时会检查 returnKeyType 和其他属性,但不会监视以后的更新。更改 returnKeyType 后,尝试在文本字段上调用 ​​reloadInputViews,这应该指示它刷新键盘设置。

Normally the returnKeyType and other properties are checked when the keyboard is displayed, but are not monitored for later updates. Try calling reloadInputViews on the text field after changing the returnKeyType, that should instruct it to refresh the keyboard settings.

心房的律动 2024-11-13 07:50:30

不幸的是,-[reloadInputViews] 会弄乱两字节键盘上的键盘输入。

双字节键盘(例如汉字)将临时字符放入文本字​​段中,并要求用户按 Enter 键进行辅助击键以确认输入的字符。调用 -[reloadInputViews] 会放弃辅助击键状态。

我还没有找到针对这种情况的解决方案。

Unfortunately, -[reloadInputViews] will mess up keyboard input on two-byte keyboards.

Two-byte keyboards (Kanji for example) put temporary characters in the text field and require the user to hit enter a secondary keystroke to confirm the character entered. Calling -[reloadInputViews] abandons the secondary keystroke state.

I haven't yet found a solution for this scenario.

一袭白衣梦中忆 2024-11-13 07:50:30

有些人建议在 textField 上调用 reloadInputViews 来强制更新键盘的返回键。不幸的是,当您在文本字段上调用 ​​reloadInputViews 时,键盘会重置,这会将其更改回文本字段的默认值,例如,从数字输入切换到电子邮件输入。您可以通过仅在 returnKeyType 更改时调用 reloadInputViews 来最大程度地减少发生这种情况的频率。

int currentTextFieldReturnKeyType = textField.returnKeyType;
[textField setReturnKeyType:UIReturnKeyGo];
if (currentTextFieldReturnKeyType != textField.returnKeyType ) {
    [textField reloadInputViews];
}

这并不能完全解决问题,但可以将问题最小化到回车键发生变化的一次击键。

Some have proposed calling reloadInputViews on the textField to force the keyboard's return key to update. Unfortunately the keyboard resets when you call reloadInputViews on the textField, which changes it back to the textField's default, e.g., switching from number entry to email entry. You can minimize how often this happens by only calling reloadInputViews IF the returnKeyType changes.

int currentTextFieldReturnKeyType = textField.returnKeyType;
[textField setReturnKeyType:UIReturnKeyGo];
if (currentTextFieldReturnKeyType != textField.returnKeyType ) {
    [textField reloadInputViews];
}

This doesn't completely solve the problem, but minimizes it to the one keystroke where the return key changes.

心的憧憬 2024-11-13 07:50:30

正如 Animie 所说,最好的方法是 reloadInputViews 告诉键盘有关更改的信息。

您可以这样做:

- (void)textFieldDidBeginEditing:(UITextField *)textField{

if(self.myTextField.editing){

      self.myTextField.editing.returnKeyType = UIReturnKeyNext;// or UIReturnKeyGo, etc.

   }
}

在代码中的某个位置,例如在 viewDidLoad 中,注册 UIKeyboardWillShowNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

当键盘出现时,您重新加载InputViews

- (void)keyboardWillShow:(NSNotification *)notification{
    [self.myTextField reloadInputViews];

}

As Animie said, the best way is to reloadInputViews to tell the keyboard about the changes.

You can do like this:

- (void)textFieldDidBeginEditing:(UITextField *)textField{

if(self.myTextField.editing){

      self.myTextField.editing.returnKeyType = UIReturnKeyNext;// or UIReturnKeyGo, etc.

   }
}

Somewhere in your code, in viewDidLoad for example, register for UIKeyboardWillShowNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

And when the keyboard shows up, you reloadInputViews:

- (void)keyboardWillShow:(NSNotification *)notification{
    [self.myTextField reloadInputViews];

}
梅倚清风 2024-11-13 07:50:30

正如大家所说,reloadInputViews 将键盘重置为其初始 UIKeyboardType。 Apple 应该为我们提供一个 API 来询问键盘的键盘类型,以将其设置回重新加载输入视图之前的状态。

使用新的 iOS8 API,您可以创建自己的键盘并返回 UIKeyboardType。

如果仍然使用 iOS7,您可以询问某个键盘子视图处于哪种状态。存储该值并在需要时重置。在这里查看我的来源:https://github.com/ustwo/US2KeyboardType

As you all say, reloadInputViews resets the keyboard to its initial UIKeyboardType. Apple should give us an API to ask the keyboard for its keyboard type to set it back to what it was before reloading the input views.

With the new iOS8 API you can create your own keyboard returning the UIKeyboardType.

If still using iOS7 you can ask a certain keyboard child view in which state it is. Store that value and reset whenever you need to. Have a look at my source here: https://github.com/ustwo/US2KeyboardType

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文