1

我有以下 UISearchBar 委托方法,它自己工作(它在单独时被调用)。

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self downloadFruits:searchBar.text];
    [self.view endEditing:YES];
}

当我添加另一个 UISearchBar 委托方法时,前一个委托方法停止工作(不再调用它)。

#define CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz _-."
#define CHARACTERS_NUMBERS  [CHARACTERS stringByAppendingString:@"1234567890"]

-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSCharacterSet *unacceptedInput =
    [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS_NUMBERS] invertedSet];
    // If array has more than one entry, there was at least one unacceptable character
    if ([[text componentsSeparatedByCharactersInSet:unacceptedInput] count] > 1)
        return NO;
    else
        return YES;
}

它们中的每一个都可以单独使用,但不调用第一个(搜索按钮)

4

2 回答 2

3

我认为您应该将 \n 添加到 CHARACTERS

#define CHARACTERS @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz _-.\n"
于 2013-02-26T17:54:17.730 回答
0

我认为第二个方法总是返回NO,这使得第一个没有被调用

于 2013-02-26T12:45:34.443 回答