I'm trying to add a UIViewController subview, and then upon clicking a button close it. My current code does the job, I'm just not sure if it will leak or cause any problems.
So first I add the subview
-(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];
}
Then in newViewController.m I have the function to remove it
-(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];
}
Like I said this works, however when I Analyze the code, it tells me:
Potential leak of an object allocated on line X and stored into 'newViewController' for:
newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
and
Incorrect decrement of the reference count of an object that is not owned at this point by the caller for [self.view release];
If I autorelease
the viewController instead of [self.view release]
it crashes upon removing (also if I release the view after adding it) with: -[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xd21c7e0
If I call [newVC release]
in either viewControllers dealloc
it fails to build.
Hopefully I'm not asking a fairly obvious question, but what is the correct way to add and remove viewControllers?