搜索后重新加载视图会导致应用崩溃

时间:2012-01-06 16:14:33

标签: ios uitableview uisearchbar nsfetchedresultscontroller

我正在创建一个包含UITableView UISearchDisplayController视图的应用。数据来自NSFetchedResultsController。一切正常:我获取数据,正在填充表视图,搜索效果很好。唯一的问题是,如果我搜索然后单击“取消”(不删除UISearchBar中的文本),然后返回到上一个视图控制器,然后使用UITableView和{{1}转到相同的视图它被写入日志时崩溃了:

  

2012-01-06 16:46:37.559 MyApp [9586:207] *** - [SomeRandomViewController   controllerWillChangeContent:]:发送到解除分配实例的消息   0x778d060

我已经谷歌搜索并且堆栈溢出了该错误,试图释放UISearchDisplayController变量,确实设置了它并且它的'委托为零,但没有任何帮助。

如果我进行一些搜索,然后从搜索栏中删除文本,然后点击“返回”并返回该视图,它就可以正常工作。

一些代码:

NSFetchedResultsController

任何帮助将不胜感激

更新

没有僵尸,日志的输出是:

  

2012-01-09 09:25:48.128 Butane [17325:207]    - [UITextMagnifierTimeWeightedPoint controllerWillChangeContent:]:无法识别的选择器发送到实例0x8251dc0

更新2

Dealloc方法:

- (void)viewDidUnload {
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

  self.fetchedResultsController = nil;
  self.searchFetchedRC = nil;
  [super viewDidUnload];
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {

  self.fetchedResultsController = nil;
  [self fetchedResultsController];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  self.searchDisplayController.searchBar.text = @"";
  self.searchDisplayController.active = NO;
  [self searchDisplayControllerWillEndSearch:self.searchDisplayController];
  return YES;
}

更新3

受到Tia答案的启发,我在- (void)dealloc { [self.mySearchDisplayController release]; self.mySearchDisplayController.delegate = nil; self.mySearchDisplayController = nil; [self.fetchedResultsController release]; self.fetchedResultsController.delegate = nil; self.fetchedResultsController = nil; [self.searchFetchedRC release]; self.searchFetchedRC.delegate = nil; self.searchFetchedRC = nil; [self.tableView release]; [textView release]; self.tableView = nil; self.tableView.delegate = nil; [super dealloc]; } mySearchDisplayController = UISearchDisplayController fetchedResultsController and searchFetchedRC = NSFetchedResultsControllers tableView = UITableView textView = [HPGrowingTextView][1] 方法中将FRC及其代表设置为nil。到目前为止工作很好

searchDisplayControllerWillEndSearch

2 个答案:

答案 0 :(得分:1)

- (void)dealloc {
  //[self.mySearchDisplayController release];
  self.mySearchDisplayController.delegate = nil;
  self.mySearchDisplayController = nil;
  //[self.fetchedResultsController release];
  self.fetchedResultsController.delegate = nil;
  self.fetchedResultsController = nil;
  //[self.searchFetchedRC release];
  self.searchFetchedRC.delegate = nil;
  self.searchFetchedRC = nil;
  //[self.tableView release];
  [textView release];
  self.tableView.delegate = nil;
  self.tableView = nil;
  [super dealloc];
}

您过度释放了自己的属性,因为将其设置为nil已经为保留的属性释放了它。我试图通过评论删除额外的释放代码,所以请尝试用上面的代码替换你的dealloc并查看。

答案 1 :(得分:1)

我有一个类似的问题,因为我的VC实现了UISearchBarDelegate协议和searchBarTextDidEndEditing:在之后被称为我在tableView:didSelectRowAtIndexPath:方法中解除了VC。

我修复了:

self.searchDisplayController.delegate=nil;
self.searchDisplayController.searchBar.delegate=nil;

之前的

[self dismissModalViewControllerAnimated:YES];