2012-01-06 83 views
0

我正在制作一个应用程序,其视图UITableViewUISearchDisplayController。数据来自NSFetchedResultsController。所有工作都很好:我得到的数据,表格视图正在填充,搜索很好。唯一的问题是,如果我搜索然后单击“取消”(不删除UISearchBar中的文本),然后返回到上一个视图控制器,然后转到与UITableViewUISearchDisplayController相同的视图,它会崩溃并写入日志:搜索后重新加载视图崩溃应用程序

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

我因为这个错误已经搜索了并且堆栈了,试图发布NSFetchedResultsController变量,确实设置了它,并且它的'deleg吃零,但没有任何帮助。

如果我做一些搜索,然后从搜索栏中删除文本,然后点击“返回”,并返回到该视图它工作得很好。

码的位:

- (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] - [UIText MagnifierTimeWeightedPoint 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我的答案的启发已设置FRC及其代表无searchDisplayControllerWillEndSearch方法。作品到目前为止好

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

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

[启用僵尸(http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in:

我与固定它-xcode)并返回给我们。 – Joe 2012-01-06 16:23:01

+0

http://stackoverflow.com/questions/3532861/core-data-app-crashing-with-controllerwillchangecontent-unrecognized-selector不会帮助吗? – tia 2012-01-06 17:16:55

+0

@tia感谢您的链接,但它没有帮助 – Novarg 2012-01-09 08:19:24

回答

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并查看。

+0

感谢您的回答,但它仍然是一样的:2012-01-09 10:46:01.154 MyApp [19078:207] *** - [SomeRandomViewController controllerWillChangeContent :]:发送到释放实例的消息0x7790820 – Novarg 2012-01-09 09:47:32

+0

受您的答案的启发我在searchDisplayControllerWillEndSearch方法中将NSFetchedResultsControllers及其代表设置为nil。到目前为止它正在工作。感谢您将我推向正确的方向:) – Novarg 2012-01-09 10:15:18

+0

不用担心,但您应该真正研究关于内存管理的文档,因为它对于iOS开发非常重要。 – tia 2012-01-09 10:37:23

1

我也有类似的问题,因为我的VC实现的UISearchBarDelegate协议和searchBarTextDidEndEditing:在被称为后我会驳回了VC中的tableView:didSelectRowAtIndexPath方法:方法。

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

[self dismissModalViewControllerAnimated:YES];