2011-03-04 64 views
0

我有一個UIViewController的子類也實現了UITableViewDelegate。
基本上,有一個UITableView顯示,並選擇一個單元格推新視圖。
以下是有關代碼看起來:objective C無法釋放UIViewController

// NavigationContentsViewController.h 
@interface NavigationContentsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    IBOutlet UITableView * newsTable; 
    UIActivityIndicatorView * activityIndicator; 
} 

/////////////////////////////////////////////////////////////////////// 
// placed in NavigationContentsViewController.m 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSInteger selectedCellItem = indexPath.row; 

    TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]]; 
    fvController.selectedCellItem = selectedCellItem; 
    fvController.link = [links objectAtIndex:selectedCellItem]; 
    [self.navigationController pushViewController:fvController animated:YES]; 
    [fvController release]; // #1 if removed everything works fine but getting a leak 
    fvController = nil;  // #2 
} 
/////////////////////////////////////////////////////////////// 
// TableViewController.h 
@interface TableViewController : UIViewController <UIWebViewDelegate>{ 
    NSInteger selectedCellItem; 
    IBOutlet UIWebView *myWebView; 
    NSString *link; 
    UIActivityIndicatorView *activityIndicator; 
} 

所以,當用戶選擇了一個小區,一個新的UIWebView推。
還有一個活動指示器顯示,直到webView完成加載。

我的問題是,如果用戶選擇在webView完成加載之前從webView回到前一個視圖(帶有表格的視圖),則應用程序崩潰,出現此錯誤:「[TableViewController respondsToSelector:] :消息發送到釋放實例0x4b57460「

如果webView已完成加載,並且用戶選擇返回一切正常。

我設法解決這個問題,通過刪除代碼中標記爲#1和#2的兩行,但然後我得到一個內存泄漏(因爲我不釋放TableViewController)。

仍試圖瞭解發生了什麼。有任何想法嗎?提前致謝。

回答

4

我懷疑你是設置你的TableViewController爲代表的UIWebView中,你錯過了the documentation此消息:

Important: Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil . This can be done, for example, in your dealloc method.

如果忘記取出在dealloc的委託,web視圖最終可能會嘗試即使委託不再存在,也要發送委託消息。繁榮。

+0

這正是我正在做的,這正是我所缺失的......非常感謝回覆!接受的答案;) – CrisDeBlonde 2011-03-04 00:33:27