首先我加了我的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 –