【发布时间】:2012-04-30 04:29:13
【问题描述】:
我正在尝试添加 UIViewController 子视图,然后单击按钮将其关闭。我当前的代码可以完成这项工作,我只是不确定它是否会泄漏或导致任何问题。
所以我首先添加子视图
-(IBAction)openNewView:(id)sender{
// start animation
[UIView beginAnimations:@"CurlUp" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// add the view
newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:newVC.view];
[UIView commitAnimations];
}
然后在 newViewController.m 我有删除它的功能
-(IBAction)closeNewView:(id)sender{
// start animation
[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
// close dialog
[self.view removeFromSuperview];
[UIView commitAnimations];
[self.view release];
}
就像我说的那样有效,但是当我分析代码时,它告诉我:
在第 X 行分配并存储到“newViewController”中的对象的潜在泄漏:
newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
和
[self.view release]; 的调用者此时不拥有的对象的引用计数的递减不正确
如果我 autorelease viewController 而不是 [self.view release] 它在删除时崩溃(如果我在添加视图后释放视图):-[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xd21c7e0
如果我在任一 viewControllers dealloc 中调用 [newVC release],则构建失败。
希望我不是在问一个相当明显的问题,但是添加和删除 viewControllers 的正确方法是什么?
【问题讨论】:
标签: ios uiviewcontroller release-management addsubview memory-management