实现UISearchBar委托方法后,键盘没有响应 [英] Keyboard is not responding after implementing UISearchBar delegate method

查看:344
本文介绍了实现UISearchBar委托方法后,键盘没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UISearchBar 我在 viewDidLoad 中实现:按代码。
我还设置了 UISearchBarDelegate

I have an UISearchBar which i implemented in my viewDidLoad: by code. I have also set the UISearchBarDelegate.

现在我想限制用户输入超过5个字符所以我实现了这个委托方法

Now i want to restrict the user from entering more than 5 chracter So i implement this delegate method

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    NSLog(@"shouldChangeTextInRange");

    if (searchBar.text.length >= 5)
        return NO;

    return YES;
}

工作正常。

问题是当我输入最多5个字符&尝试使用键盘Backspace字符,它不工作。

The problem is when i typed upto 5 chracters & try to use the keyboard Backspace character, it is not working.

现在如果我按下键盘上的搜索按钮 searchBarSearchButtonClicked :没有被调用。

Also now if i pressed Search button in keyboard the searchBarSearchButtonClicked: is not getting called.

我目前正在使用

XCode版本:3.2.5

XCode version :3.2.5

iOS SDK:4.2

iOS SDK :4.2

推荐答案

您应该对文字进行测试长度(如果应用建议的文本更改,您将拥有的文本的长度),实际文本长度。

You should do your test on the new text length (then length of the text that you will have if the suggested text change is applied), not the actual text length.

为此,首先需要计算新文本:

For that, you first need to compute the new text :

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
        return YES; // accept validation button

    NSString* newText = [searchBar.text stringByReplacingCharactersInRange:range withString:text];

    if (newText.length >= 5)
        return NO;

    return YES;
}

这篇关于实现UISearchBar委托方法后,键盘没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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