2

如果我在编辑作为 UINavigationBar 的 titleView 嵌入的 UISearchBar 时单击导航控制器中的后退按钮,我会看到我的应用程序崩溃的错误。主 VC 是一个 UITableViewController,它使用[parentView.navigationController pushViewController:myTableView animated:YES];

这是我在 viewDidLoad 中创建 UISearchBar 的代码:

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

这些是我对 UISearchBar 委托的委托实现——处理搜索只是更新支持 tableView 的数组并调用 [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];
}

我没有从崩溃中得到任何信息——只是一个信号。如果我不编辑 UISearchBar 它工作正常。我已经尝试让第一响应者辞职,但它仍然崩溃。

更新 - 添加过滤数据

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

我已经在 viewWillDisappear 中尝试了以下所有 + 所有这些。它们都成功运行,并且 searchBar 引用指向有效的 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];
}

这可能不是搜索栏的问题,我只是在编辑搜索栏时始终看到崩溃-可能是视图隐藏了键盘并尝试在解构 TableView 的同时重绘下面的单元格.

4

0 回答 0