Hi all,
I can't figure out why I'm getting inconsistent results from nearly identical code. I have two viewcontrollers that I'm loading as needed. As I understand it, addSubview is supposed to increase the retain count, but it is doing that in one method and not another. It's a problem because I'm calling release and expecting the viewController to tear down, but in one case it is and in another there's still a retainCount left keeping the VC alive...
I should add that in view2, I've got an NSTimer running, which starts during viewDidLoad. Would that be the culprit? If so, why would it retain its parent object?
What am I missing here?
Code:
-(void)gotoView1 {
newViewController = [[ViewController1 alloc] initWithNibName:@"View1" bundle:nil];
NSLog([NSString stringWithFormat:@"%@: View1 count is %d", [newViewController description] ,[newViewController retainCount]]);
[[newViewController view] setAlpha:0];
[[newViewController view] setBounds:CGRectMake(0,0, 480, 320)];
[window addSubview:[newViewController view]];
NSLog([NSString stringWithFormat:@"%@: View1 count is %d", [newViewController description] ,[newViewController retainCount]]);
[UIView beginAnimations:@"windowFlip" context:NULL];
[UIView setAnimationDuration:0.5];
[[newViewController view] setAlpha:1];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
//ASSIGN DATA STUFF HERE
}
-(void)gotoView2{
newViewController = [[ViewController2 alloc] initWithNibName:@"View2" bundle:nil];
NSLog([NSString stringWithFormat:@"%@: View2 count is %d", [newViewController description] ,[newViewController retainCount]]);
[[newViewController view] setAlpha:0];
[[newViewController view] setBounds:CGRectMake(0,0, 480, 320)];
[window addSubview:[newViewController view]];
NSLog([NSString stringWithFormat:@"%@: View2 count is %d", [newViewController description] ,[newViewController retainCount]]);
[UIView beginAnimations:@"window2Flip" context:NULL];
[UIView setAnimationDuration:0.5];
[[newViewController view] setAlpha:1];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
//ASSIGN DATA STUFF HERE
}
The NSLog output looks like this:
<ViewController1: 0x116f40>: View1 count is 1
<ViewController1: 0x116f40>: View1 count is 1
<ViewController2: 0x15fbf0>: View2 count is 1
<ViewController2: 0x15fbf0>: View2 count is 2