如何以编程方式将键盘视图从“符号"重置为“字母" [英] How to reset keyboard view from 'symbols' to 'alphabet' programmatically

查看:40
本文介绍了如何以编程方式将键盘视图从“符号"重置为“字母"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView,我正在监听一个 UIButton 事件以对该 textView 中的文本采取行动:

I have a UITextView and I am listening to a UIButton event to take action on the text inside this textView:

- (IBAction)buttonClicked {
    NSString *text = myTextView.text;
    //Do some work with the text.
    myTextView.text = @""; //reset the textView.
}

我不希望 textView resignFirstResponder.我面临的问题是:当最后输入的字符是一个符号时,键盘卡在该视图上并且不会重置为默认键盘字母视图,消息应用程序和其他应用程序中的行为也是如此.如何在此方法中以编程方式重置键盘?

I don't want the textView to resignFirstResponder. The problem I am facing is: When the last entered character was a symbol, the keyboard is stuck on that view and is not reset to the default keyboard alphabetic view, as is the behavior in messaging app and other apps too. How do I reset the keyboard programmatically in this method?

这可能是这个问题的重复问题.但答案似乎对我不起作用.

This may be a repeat question for this question. But the answer doesn't seem to work for me.

这是公认的答案:

[editingField setKeyboardType:UIKeyboardTypeNumberPad];
[editingField reloadInputViews];

这是最后一条评论:

好的,显然将其声明为 UITextView 而不是 UIResponder 会影响它在运行时的行为方式.这实际上是我所做的唯一更改,现在可以使用了.谢谢.

OK, apparently declaring it as a UITextView instead of a UIResponder affects how it behaves during runtime. That is literally the only change I made and it works now. Thanks.

但我似乎无法理解是什么让它起作用了,因为我仍在努力解决这个问题.

But I can't seem to understand what the small change was that made it work, since I am still struggling with the issue.

我已经尝试过resignFirstResponderbecomeFirstResponder,但是很快,后面的scrollableView 没有根据keyboardWillShow 事件设置.我也试过 [textView setKeyboardAppearance:UIKeyboardAppearanceDefault] 但仍然没有变化.

I have already tried resignFirstResponder and becomeFirstResponder, but in quick succession, the scrollableView behind is not set according to keyboardWillShow event. I have also tried [textView setKeyboardAppearance:UIKeyboardAppearanceDefault] but there is no change still.

是否有任何 API 方法可以执行此操作(例如在单击空格"时自动重置键盘)?

Is there any API method that does this thing (like the keyboard is reset on clicking 'space' automatically)?

推荐答案

我使用的一个技巧是使用隐藏的 textField 重新加载 inputView.

A hack that I used was to use a hidden textField to reload the inputView.

代码:

UITextField *hiddenTextField = [[UITextField alloc] init];
[superView addSubview:hiddenTextField];

[hiddenTextField becomeFirstResponder];
[hiddenTextField reloadInputViews];
[textView_ becomeFirstResponder];

[hiddenTextField removeFromSuperview];
[hiddenTextField release];

这篇关于如何以编程方式将键盘视图从“符号"重置为“字母"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆