“[NSBigMutableString substringWithRange:]: Range {0, 10} out of bounds; string length 9” error with Undo

早过忘川 提交于 2019-11-29 14:01:46

You should be using this delegate method instead:

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

And just return NO in case the replacementText is equal to "%". This will prevent the user from using it in the first place as it won't update the textfield, which should fix the undo issue you're having.

Solution

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"%"]) {
        return NO;
    }

    return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!