目标C - 在添加shouldChangeTextInRange后未调用的UIsearchBar的searchBarSearchButtonClicked委托

时间:2014-08-29 08:22:42

标签: ios objective-c uisearchbar uisearchbardelegate

我正在尝试实施 UISearchBar 委托

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

    NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzQWERTYUIOPLKJHGFDSAZXCVBNM0123456789!@#$%^&*()_+./?'\"\\*- "] invertedSet];

    const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];

    string = [[string componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];

    int isBackSpace = strcmp(_char, "\b");

    if((string!=nil && ![string isEqualToString:@""]) || isBackSpace == -8)
    {
        return YES;
    }

    return NO;

}

并点击搜索按钮

-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBarSender{[mySearchBar resignFirstResponder];

}

searchBarSearchButtonClicked 委托永远不会被解雇。

我尝试删除 shouldChangeTextInRange 然后调用它。我做错了什么?

1 个答案:

答案 0 :(得分:0)

找到答案Here

这是因为我没有在允许的字符中传递 \ n 字符,而搜索使用 \ n 来调用。