我正在尝试添加 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
如果我调用[newVC release]
任何一个 viewController dealloc
,它都无法构建。
希望我不是在问一个相当明显的问题,但是添加和删除视图控制器的正确方法是什么?