首先我加了我的ViewController以NavigationController這樣,如何在添加到NavigationController時釋放ViewController對象?
SecondViewController *viewControllerObj = [[SecondViewController alloc] init];
[self.navigationController pushViewController:viewControllerObj animated:YES];
[viewControllerObj release];`
但這創建崩潰,當我在導航欄上按後退按鈕後,被推到SecondViewController。所以我創建SecondViewController對象在.H文件屬性,並以這樣的dealloc釋放 viewControllerObj,
現在我將我的ViewController以NavigationController這樣,
在.h文件中,
@property (nonatomic,retain) SecondViewController *viewControllerObj;
在.m文件,
self.viewControllerObj = [[SecondViewController alloc] init];
[self.navigationController pushViewController:self.viewControllerObj animated:YES];
[_viewControllerObj release]; // in dealloc method
當我分析我的項目這表明潛在的泄漏對viewControllerObj,所以我已經修改了我這樣的代碼,
SecondViewController *temp = [[SecondViewController alloc] init];
self.viewControllerObj = temp;
[temp release];
[self.navigationController pushViewController:self.viewControllerObj animated:YES];
現在我清除潛在的泄漏
但我不知道這是否是正確與否,是每個viewController對象需要聲明爲Property並在dealloc中釋放?
即使我試圖與自動釋放不起作用 –
確保你不應該在發佈對象中刪除發佈聲明,如果任何代碼或dealloc方法。 –
檢查也@Bhargavi –