ios - 尽管设置了委托(delegate),但未调用 searchBarSearchButtonClicked

标签 ios ios7 uisearchbar uisearchbardelegate

好吧,我正在努力解决这个问题。我几乎可以肯定,我曾经也曾使用过此功能,但现在却无法使用。

我在表格 View 单元格中嵌入了一个 UISearchBar。我将搜索栏声明为属性,我的 View Controller 设置为 UISearchBarDelegate。搜索栏已初始化、配置,一切看起来都很棒。它响应其他委托(delegate)方法以限制输入的字符、处理取消按钮以及除 searchBarSearchButtonClicked 之外的所有内容。我到底做错了什么?

这是我的设置的基础:

ViewController.h

    @interface SearchMLSNumberViewController : UIViewController <UISearchBarDelegate>

    @property (strong, nonatomic) UISearchBar *searchField;

ViewController.m

// viewDidLoad
// init the search bar
self.searchField = [[UISearchBar alloc] init];
self.searchField.delegate = self;
self.searchField.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchField.placeholder = NSLocalizedString(@"Search", @"Search");

// in my cell setup
CGRect frame = cell.contentView.frame;
[self.searchField setFrame:frame];
[cell.contentView addSubview:self.searchField];

// here is the delegate call that never fires for me
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"search button pressed");
}

我已经排除了我所有的 tableview 设置和所有这些,它似乎工作正常。那就是要了我的命。一切正常,所有其他委托(delegate)方法......除了搜索按钮返回。请告诉我我正在做一些愚蠢的事情。我别无选择。

更新 我已经弄清楚为什么搜索按钮无法执行的罪魁祸首了。我删除了所有其他搜索栏委托(delegate)方法,它开始工作了……所以我将它们全部注释掉,直到我发现是哪一个导致了问题。

这家伙是我的罪魁祸首:

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,-"] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
    return [text isEqualToString:filtered];
}

那么现在,搜索按钮是否真的算作一次按键,我需要将它添加到我允许的字符中吗?或者我需要为它添加支票以便允许返回吗?任何帮助都会很棒。

更新 2 我已经解决了我的问题。它结合了线索和启示,但我终于让它发挥作用了。罪魁祸首是不允许字符集中的返回键。我将\n 添加到字符集,瞧,搜索按钮无法正常工作。这是我更新的过滤器逻辑,用于显示添加的返回键,以及适合我的情况的有效字符集。一旦允许,我会自己回答这个问题。

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,-\n"] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
    return [text isEqualToString:filtered];
}

最佳答案

我在这里回答我自己的问题,这样我就可以把事情了结了。我在提出问题后不久就解决了这个问题。有趣的是如何工作。答案与其他用户相关,因此我会为后代更新我的答案。

tl;dr 是我不允许 -shouldChangeTextInRange 中的返回键。我有一组有限的允许输入的字符,我忘记将返回键作为有效字符。

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,-\n"] invertedSet];
    NSString *filtered = [[text componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
    return [text isEqualToString:filtered];
}

如果您也在限制文本字段中的字符,这也可能会起作用。这就是我最终解决问题的方式。我首先删除了我所有的委托(delegate)方法,直到我发现是哪个委托(delegate)方法阻止了我。一旦弄清楚我在做什么,我就会通过搜索文本字段问题来追踪返回键。搜索栏没有太多问题,这就是为什么我最终问了这个问题。我可能在不问问题的情况下就明白了这一点,但至少我学到了一些东西,希望这个答案将来能帮助其他人。

另外,感谢@mattt 就限制字符的更好方法和思考我的代码库的整体健康状况提出的建议。

关于ios - 尽管设置了委托(delegate),但未调用 searchBarSearchButtonClicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968483/

相关文章:

ios - UITableviewCell textLabel在iOS7中不可见

ios - 我将如何在 iOS 的谷歌地图中添加搜索栏

使用 Core Data 和 NSFetchedResultsControllers 使用 TableViewControllers 搜索后添加新条目时 iOS 应用程序崩溃

iphone - iOS 7 UIButton 仅在点击其文本标签时响应

objective-c - iOS7 Multipeer Connectivity 是否支持经典蓝牙或 BLE 4.0?

ios - Swift 错误 Anyobject 没有名为 'Generator' 的成员

iOS NSNotificationCenter 观察者未被删除

ios - UiSearchBar 用于 map ,例如使用 Swift 进行地址搜索

ios - 无法安装 PhoneGap macOS(桌面应用程序)

iphone - 用于删除 HTML-head-tag 的正则表达式