当 UIKeyboard 处于视图中时,是否可以更改 UITextInputTraits?
我想在使用键盘时更改键盘的 UITextInputTraits...
我的理想代码看起来像这样:
- (IBAction)nameTextDidChange:(UITextField *)sender {
if ([sender.text isEqualToString:@""]) {
sender.returnKeyType = UIReturnKeyDone;
} else {
sender.returnKeyType = UIReturnKeySearch;
}
}
所以...我有一个不同的“返回”按钮用于空字符串当我做一个带有一些文本的字符串时。我上面发布的代码不起作用,键盘保留了它的原始文本输入特征。
任何人有什么想法,或者无论我多么努力,这都永远不会起作用吗?
干杯!
缺口。
感谢 Deepak,这是我实际使用的代码:
if ([sender.text isEqualToString:@""]) {
sender.returnKeyType = UIReturnKeyDone;
[sender resignFirstResponder];
[sender becomeFirstResponder];
} else if (sender.returnKeyType == UIReturnKeyDone) {
NSString *cachedLetter = sender.text;
sender.returnKeyType = UIReturnKeySearch;
[sender resignFirstResponder];
[sender becomeFirstResponder];
sender.text = cachedLetter;
}
I want to change the UITextInputTraits of a keyboard while it is in use....
My ideal code would look something like this:
- (IBAction)nameTextDidChange:(UITextField *)sender {
if ([sender.text isEqualToString:@""]) {
sender.returnKeyType = UIReturnKeyDone;
} else {
sender.returnKeyType = UIReturnKeySearch;
}
}
So... I have a different 'Return' button for an empty string as I do a string with some text in. The code I posted above doesn't work, the keyboard retains it's original text input traits.
Any ideas anyone, or is this never going to work no-matter how hard I try?
Cheers!
Nick.
Thanks to Deepak, this is the code I actually used:
if ([sender.text isEqualToString:@""]) {
sender.returnKeyType = UIReturnKeyDone;
[sender resignFirstResponder];
[sender becomeFirstResponder];
} else if (sender.returnKeyType == UIReturnKeyDone) {
NSString *cachedLetter = sender.text;
sender.returnKeyType = UIReturnKeySearch;
[sender resignFirstResponder];
[sender becomeFirstResponder];
sender.text = cachedLetter;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过在方法末尾添加以下行来实现此目的。
这应该有效。
您基本上可以打开和关闭它,以便文本输入发生变化。第二个“如果”是确保仅在需要时翻转。
You can make this work by adding the following lines at the end of the method.
This should work.
You basically flip it on and off so that the text input changes. The second if is to make sure you flip only if needed.
[textField reloadInputViews]
似乎可以解决问题......[textField reloadInputViews]
seems to do the trick...辞职第一响应者/成为第一响应者 & reloadInputViews 有一些注意事项。请参阅 stackoverflow.com/questions/5958427/…。这些会将键盘更改回其初始状态。因此,如果用户切换到其他键盘布局之一(数字、标点符号等),该状态将会丢失。我一直没有找到好的解决办法。
resignFirstResponder/becomeFirstResponder & reloadInputViews have some caveats. See stackoverflow.com/questions/5958427/…. These change the keyboard back to its initial state. So if the user had toggled to one of the other keyboard layouts (numbers, punctuation, etc), that state will get lost. I've never found a good solution.