2

I am seeing a bug where my app crashes if I click the back button in a navigation controller while editing a UISearchBar embedded as the titleView of the UINavigationBar. The main VC is a UITableViewController that is pushed onto the view stack using [parentView.navigationController pushViewController:myTableView animated:YES];

Here is the code I use to create the UISearchBar in my viewDidLoad:

UISearchBar *customSearch = [[UISearchBar alloc] initWithFrame:
                                CGRectMake(0,0, 320, 44)];
customSearch.delegate = self;
customSearch.placeholder = @"Some placeholder text";
self.navigationItem.titleView = customSearch;

These are my delegate implementations for the UISearchBar delegate - handle search just updates the array backing the tableView and calls [self.tableview reloadData]:

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
}
- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self handleSearch:searchBar];
    [searchBar resignFirstResponder];
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self handleSearch:searchBar];
}

- (void)handleSearch:(UISearchBar *)searchBar {
    [self updateFilteredData:searchBar.text];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
    searchBar.text = @"";
    [self handleSearch:searchBar];
    [searchBar resignFirstResponder];
}

I don't get any information from the crash - just a sigkill. If I'm not editing the UISearchBar it works fine. I've tried resigning the first responder and it still crashes.

Update - adding filtered data

- (void) updateFilteredData: (NSString *) nameFilter {
    if (nameFilter.length) {
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(first_name CONTAINS[cd] %@) OR (last_name CONTAINS[cd] %@)", nameFilter, nameFilter];
      self.filteredData = [self.data filteredArrayUsingPredicate:predicate];
    } else {
        self.filteredData = self.data;
    }
    [self.tableView reloadData];
}

I've tried all of the following + all of them together in viewWillDisappear. They all run successfully and the searchBar reference is to a valid UISearchBar.

-(void)viewWillDisappear:(BOOL)animated {
    UISearchBar *mySearchBar = (UISearchBar *)self.navigationItem.titleView;
    [mySearchBar resignFirstResponder];
    mySearchBar.delegate = nil;
    self.navigationItem.titleView = nil;
    for (UIView *view in [mySearchBar subviews] ) {
        [view removeFromSuperview];
    }
    [mySearchBar removeFromSuperview];
    [super viewWillDisappear:animated];
}

It may not be a search bar thing, I just see the crash consistently when I'm editing the search bar - It could be something with the view hiding the keyboard and trying to redraw the cells below at the same time the TableView is being deconstructed.

Ted Tomlinson
  • 773
  • 2
  • 7
  • 18
  • I'm guessing this may be related to http://stackoverflow.com/questions/15627419/strange-crash-when-dismissing-view-controller-auto-layout-to-blame, but would love advice on where to look into the auto-layout constraints. – Ted Tomlinson Jul 19 '13 at 23:56
  • 1
    I copied and pasted your code, and I got no crash. My updateFilteredData method was empty, so it could be something you're doing there causes the crash. Try commenting out the code in that method, and see if the crash goes away. – rdelmar Jul 20 '13 at 05:43
  • Thanks for the response - I added the updateFilteredData function. I have a breakpoint at the top of that function that never gets triggered before the crash so I don't think that is where the crash is happening. I think it may be with how the system is adding the cancel button to the search bar with `searchBar.showsCancelButton = NO;` I'm going to see what happens if I leave it permanently in place. Update - still see the crash even with permanent cancel button. – Ted Tomlinson Jul 20 '13 at 18:25
  • What happens if you unset the searchBar's delegate (set it to nil) on the viewWillDisappear method? I'm wondering if the search bar is calling the ...DidEndEditing method after your VC has been dealloc'd? – OC Rickard Jul 20 '13 at 18:58
  • That was one of the first things I looked at I've tried all of the following + all together in viewWillDisapear - see update since comments don't allow for well formatted multiline code. – Ted Tomlinson Jul 20 '13 at 21:51

0 Answers0