2

我正在寫一個iPhone應用程序。從導航堆棧[稱爲EditCreatorController]中的視圖控制器開始,我將呈現一個定製的模式視圖控制器[稱爲BMSStringPickerController]。我已經創建了一個委託協議等,按照Apple的指導原則將數據傳遞迴第一個視圖並使用該視圖來消除模態視圖。我甚至可以從模態控制器獲得預期的數據,並能夠很好地解決它。問題是,在這一點上,幾乎我取原視圖控制器上執行任何操作導致調試器的錯誤等解散模態視圖後,父視圖似乎解除分配?

- [EditCreatorController performSelector:withObject:withObject:]:消息發送到釋放的實例0x3a647f0

- [EditCreatorController的tableView:willSelectRowAtIndexPath:]:發送到釋放的實例0x3c12c40

換句話說消息時,它看起來像原始視圖控制器已蒸發,而模態的視圖被示出。無論這兩個委託回調中哪一個被調用,都是如此。

下面是從調用模式視圖父控制器代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row == 1) { // selection on creator type row 

    // create a string picker to choose new creator type from list 
    BMSStringPickerController *picker = [[BMSStringPickerController alloc] initWithNibName:@"BMSStringPickerController" bundle:nil]; 
    picker.delegate = self; 
    picker.stringChoices = [NSArray arrayWithObjects:@"composer", @"lyricist", @"arranger", @"original artist", @"other", nil]; 
    picker.currentChoice = creator.type; 
    picker.title = @"Creator Type"; 

    // wrap it in a nav controller so we can get tile bar etc. (from VC prog guide p. 93) 
    UINavigationController *newNavigationController = [[UINavigationController alloc] 
                initWithRootViewController:picker]; 

    [self.navigationController presentModalViewController:newNavigationController animated:YES]; 
    [newNavigationController release]; 
    [picker release]; 

} 
} 

,這裏是委託回調:

- (void)stringPickerController:(BMSStringPickerController *)picker didPickString:(NSString *)string { 
NSLog(@"received string back: %@", string); 
typeLabel.text = string; // only change the label for now; object only changes if done button pressed 
[self.tableView reloadData]; 
[self dismissModalViewControllerAnimated:YES]; 
} 

- (void)stringPickerControllerDidCancel:(BMSStringPickerController *)picker { 
NSLog(@"picker cancelled"); 
[self dismissModalViewControllerAnimated:YES]; 
} 

另一個奇怪的事情(也許是一個線索)是,雖然我收到「收到的字符串」NSLog消息,並將其分配給typeLabel.text(typeLabel是IBOutlet到我的表視圖中的一個標籤),它永遠不會出現在那裏,即使使用表重新加載。

任何人有一些想法?

回答

2

也許你會發布delegatedeallocBMSStringPickerController

+0

賓果!謝謝邁克爾。一旦你指出,那麼血腥顯而易見! 這確實提出了一個問題:如果我不在那裏發佈它,現在是否有內存泄漏?看起來我的原始調用視圖(代表)現在有一個額外的保留。或者可能不是 - 在選取器對象中的聲明是: @property(assign)id delegate; 因此,因爲它的分配不保留,我想當選擇器通過電話dismissModal消失...一切再清潔記憶明智? (顯然,我仍然在加深對obj-c內存管理的理解!):-) – 2010-07-12 23:06:20

+0

你是對的 - 一旦屬性沒有標記爲「保留」或「複製」,它就不會被保留,也不需要釋放它。你最好把它設置爲零... – 2010-07-12 23:22:54

1

它可能不會解決你的問題,但我建議告訴拾取解僱本身(委託方法),允許響應鏈正確處理解僱:

[picker dismissModalViewControllerAnimated:YES]; 
1

的默認行爲時有內存警告是釋放所有不可見的視圖控制器的視圖。因此,如果在模態視圖控制器中存在內存警告,則其父視圖控制器可能會卸載其視圖。

發生這種情況時,在視圖控制器上調用viewDidUnload,以便您可以釋放視圖中的任何引用。如果你有引用,你沒有保留,它們將在視圖被卸載時失效。也許這發生在你的情況?

有關詳細信息,請參閱UIViewController reference Memory Management部分。如果視圖當前不可見,則UIViewController方法didReceiveMemoryWarning:釋放視圖,然後調用viewDidUnload。

相關問題