隐藏键盘 [英] Hiding the Keyboard

查看:98
本文介绍了隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UISearchBar ,在委托方法中,当文本字段被清除时隐藏键盘:

 <$ cNumClass(@filter:%@,filterText)。 

if([filterText length] == 0){
NSLog(@hiding keyboard);
[filterBar resignFirstResponder];

现在,当我使用退格按钮清除搜索字词一切都是好的。当搜索变为空时,键盘隐藏。当我按下交叉按钮以完全清除搜索字段时,不是这样。



好吧,不完全是真的。我会致电 resignFirstResponder 并隐藏键盘 - 您只是看不到它,因为它刚刚回来。我通过观察键盘显示/隐藏事件发现了这一点。



键盘怎么又显示?



我已经试图遍历 UISearchBar 的所有子视图,也可以调用



更新: / p>

事实上,我只有键盘不能禁用完成按钮:-D ...所以我会停止建议。

解决方案

我基本上同意凯文,但这不会帮助你,所以这里:



尝试循环通过searchbar的子视图,并找到类UITextField的同胞。然后将此文本字段的delegate属性设置为ViewController的类并处理回调(例如textViewShouldReturn),或直接在文本字段上调用resignFirstResponder。前者显然需要在init /加载时完成,后者可以在现有的textDidChange回调中完成。



这里有一些更多的指针:



http://讨论。 apple.com/thread.jspa?threadID=1479468&a​​mp;tstart=0
http://discussions.apple.com/thread.jspa?messageID=8176608


I have a UISearchBar and on the delegate method I hide the keyboard when the text field is cleared:

- (void)searchBar:(UISearchBar *)filterBar textDidChange:(NSString *)filterText {
    NSLog(@"filter: %@", filterText);

    if ([filterText length] == 0) {
        NSLog(@"hiding keyboard");
        [filterBar resignFirstResponder ];

Now when I use the backspace button to clear out the search term all is good. The keyboard hides when the search turns to empty. Not so when I am pressing the "cross" button to clear out the search field altogether.

Well, not entirely true. I does call resignFirstResponder and hides the keyboard - you just can't see it because it comes right back up. I found this out by observing the keyboard show/hide events.

So how come the keyboard is shown again? How can I prevent this?

I've already tried to walk all subviews of the UISearchBar and also call resignFirstResponder on those ...but unless I did something wrong - that doesn't solve this either.

Update:

In fact I just got the keyboard to not disable the "Done" button :-D ...so I will "stop" going down that road as Kevin suggested. Still I would like to know why the keyboard came back up like this.

解决方案

I basically agree with Kevin, but that doesn't help you so here goes:

Try looping through the subviews of the searchbar and find the sibling which is of the class UITextField. Then either set the delegate property of this text field to your ViewController's class and handle the callback there (e.g. textViewShouldReturn), or simply call resignFirstResponder directly on the text field. The former obviously needs to be done at init/load time while the latter can be done in your existing textDidChange callback.

Here are some more pointers:

http://discussions.apple.com/thread.jspa?threadID=1479468&tstart=0 http://discussions.apple.com/thread.jspa?messageID=8176608

这篇关于隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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