【问题标题】:Reloading view after search crashes the app搜索后重新加载视图会使应用程序崩溃
【发布时间】:2012-01-06 16:14:32
【问题描述】:

我正在制作一个具有UITableViewUISearchDisplayController 视图的应用程序。数据来自NSFetchedResultsController。一切正常:我得到了数据,正在填充表格视图,搜索效果很好。唯一的问题是,如果我搜索然后单击“取消”(不从 UISearchBar 中删除文本),然后返回上一个视图控制器,然后使用 UITableViewUISearchDisplayController 转到同一个视图,它会崩溃这正在写入日志:

2012-01-06 16:46:37.559 MyApp[9586:207] *** -[SomeRandomViewController controllerWillChangeContent:]: 发送到已释放实例的消息 0x778d060

我已经用谷歌搜索并在 stackoverflow 上搜索了那个错误,试图释放 NSFetchedResultsController 变量,确实将它和它的委托设置为 nil,但没有任何帮助。

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

一点代码:

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

任何帮助将不胜感激

更新

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

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

更新 2

Dealloc 方法:

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

更新 3

受 Tia 回答的启发,我在 searchDisplayControllerWillEndSearch 方法中将 FRC 及其代表设置为 nil。到目前为止效果很好

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

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

【问题讨论】:

  • Enable zombies 并回复我们。
  • @tia 感谢您的链接,但它没有帮助
  • @Joe 更新了没有僵尸会发生什么的问题(它们已启用)
  • 我没有看到你的dealloc。你在那里释放控制器吗?

标签: ios uitableview uisearchbar nsfetchedresultscontroller


【解决方案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 并查看。

【讨论】:

  • 感谢您的回答,但还是一样:2012-01-09 10:46:01.154 MyApp[19078:207] *** -[SomeRandomViewController controllerWillChangeContent:]: message sent to deallocated instance 0x7790820
  • 受您的回答启发,我确实在 searchDisplayControllerWillEndSearch 方法中将 NSFetchedResultsControllers 及其代表设置为 nil。到目前为止,它正在工作。感谢您将我推向正确的方向:)
  • 别担心,但是你应该认真研究一下关于内存管理的文档,因为它对于 iOS 开发非常重要。
【解决方案2】:

我遇到了类似的问题,因为我的 VC 实现了 UISearchBarDelegate 协议并且 searchBarTextDidEndEditing: 被调用 我在 tableView:didSelectRowAtIndexPath: 方法中解散了 VC。

我修复了它:

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

之前

[self dismissModalViewControllerAnimated:YES];

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 2019-06-11
    • 1970-01-01
    • 2011-09-09
    • 2015-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多