2012-01-06 74 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];