Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ddaddy

macrumors newbie
Original poster
Jun 30, 2011
5
0
Hi,
I have 2 views that switch between each other. The first time they are loaded, the viewDidLoad functions are called, however as the views are switched back and forth, they viewDidLoad is never called again.
Neither Dealloc nor viewDidUnload are called at any point.

The obvious answer is that I haven't deallocated something, however 1 of the views has just 1 button on it (to switch the views) and this is all the code it has

Code:
-(IBAction)toView2:(id)sender
{
	Slider2AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate switchToView2:self.view];
	appDelegate = nil;
	[Slider2AppDelegate release];
	[appDelegate release];
}

Any idea whats going wrong here?

Thanks
 
Code:
[Slider2AppDelegate release];

This is sending release to a class. You don't want to do this.

Code:
	[appDelegate release];

This is doing nothing because two lines about you set appDelegate to nil. And if it wasn't nil you don't want to do this. This method hasn't retained the appDelegate and hasn't obtained the appDelegate in a context that requires it to send release. See the memory management guide for details on when you should and shouldn't send retain and release.

The meat of the view switching functionality seem to be in your app delegate's switchToView2. Post that code.
 
In addition to the comments posted previously... a view doesn't get loaded every time it appears. You should consider using viewWillAppear instead of viewDidLoad.
 
The guy should set his controller pointer to release, this is the most common way of Apple.
(look at basic navigation project --> then didSelectRowAtIndexPath.)

But setting your appdelegate to nil is enough, don't need another release, Sydney was right about that (I think.)

Then to be sure, I will tell you that the guy above me is right.
You are loading the view Onto the stack, so it will remain on there (with a navigationController.) So the viewDidload only gets triggered once.
To call the things u want, u need to put it into the
Code:
- (void) viewWillAppear:(bool)animated {
 [super viewWillAppear:animated];
}

That should do it ;)
 
Thanks for the replies guys.

This is the switchToView2 code
Code:
-(void)switchToView2:(UIView *)fromView
{
	[fromView removeFromSuperview];
	[window addSubview:firstGame.view];
	NSLog(@"view1 count = %i view2 count = %i",[fromView retainCount],[firstGame.view retainCount]);
	fromView = nil;
}

Between you, you have explained a lot more to me than my book did!
So the view always stays loaded, so viewDidLoad only fires ones, but I can use viewWillAppear to reset my views.

I've followed several tutorials for switching views, and they all did it a different way. One way did infact fire viewDidLoad each time it switched, but the memory crept up each time the views where switched as if it was retaining something each time.
 
The guy should set his controller pointer to release, this is the most common way of Apple.
(look at basic navigation project --> then didSelectRowAtIndexPath.)

But setting your appdelegate to nil is enough, don't need another release, Sydney was right about that (I think.)

Why do you think he should release or nil out this object? Do you think he owns the object?
 
Last edited:
Thanks for the replies guys.

This is the switchToView2 code
Code:
-(void)switchToView2:(UIView *)fromView
{
	[fromView removeFromSuperview];
	[window addSubview:firstGame.view];
	NSLog(@"view1 count = %i view2 count = %i",[fromView retainCount],[firstGame.view retainCount]);
	fromView = nil;
}

Between you, you have explained a lot more to me than my book did!
So the view always stays loaded, so viewDidLoad only fires ones, but I can use viewWillAppear to reset my views.

I've followed several tutorials for switching views, and they all did it a different way. One way did infact fire viewDidLoad each time it switched, but the memory crept up each time the views where switched as if it was retaining something each time.

Ddaddy I would recommend reading over the memory management rules. From your code you do not have a good grasp on how it works. http://developer.apple.com/library/...a/Conceptual/MemoryMgmt/Articles/mmRules.html
 
Why do you think he should release or nil out this object? Do you think he owns the object?

Oops, I think i miswatched. Cuz that's an autoreleased appdelegate no?!
Then I told him wrong. and I thought he did a navigationContorller instead of just adding a subView. that's what I mixed it up ;)
because at the end of a navigationpush u have to release your alloc/inited pointer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.