Keyboard is not responding after implementing UISearchBar delegate method

前端 未结 2 664
抹茶落季
抹茶落季 2021-01-25 01:21

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

Now i want to rest

相关标签:
2条回答
  • 2021-01-25 02:04

    You can't use backspace because of your code. after typing 5 characters, your searchBar is stuck. Use it instead :

        if ([textField.text length] + [string length] - range.length > 5) {
            return NO;
        }
    
    0 讨论(0)
  • 2021-01-25 02:08

    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;
    }
    
    0 讨论(0)
提交回复
热议问题