Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

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

My app crashes when I try to do undo on UISearchBar. In my app I have code to prevent entering a '%' sign on the search bar and for doing this it replaces a % to @"" in textDidChange method as below:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
   self.searchBar.text = [searchText stringByReplacingOccurrencesOfString:@"%" withString:@""];
}

So if I type text "abc%xyz", the final text visible on search bar would be 'abcxyz'. No when I click undo, I see 'xyz' gets cleared and I still see 'abc' on the search bar instead of clearing 'abcxyz' all at one go.

Now if I click undo again to clear 'abc' my app crashes with [NSBigMutableString substringWithRange:]: Range out of bounds error.

I am assuming that even though '%' gets replaced by @"", the undo manager might still be holding it and hence the range is going out of bounds.

I tried [searchBar.undoManager removeAllActions]; in textDidChange after I replace % with @"", but it didn't help. Here is the code:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
       self.searchBar.text = [searchText stringByReplacingOccurrencesOfString:@"%" withString:@""];
      [searchBar.undoManager removeAllActions];
    }

Question: Anybody faced similar issue before? How do I handle the undo?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...