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

Labeno

macrumors 6502
Original poster
Jul 21, 2008
351
1,089
I've noticed that as I navigate around view controllers, that the controller will occasionally reload. This reload causes crashing and other weird issues cause it recreates all the buttons, labels, etc, and reset values back to new values. Typically I use the viewWillLoad function to create all the buttons, labels, etc, and initialize class variables.

What is the proper way to load views (buttons, labels, etc) and initialize class variables into a controller just once, so there is no worry that these will be over written due to a reload later on?

Thank you for any help.
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
You should be using viewDidLoad and/or overriding the init method. Specifically, for UIViewControllers, you can override initWithNibName:

Code:
- (id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Do your initialization here.
	}
	return self;
}

The init method is called when the class is constructed (so initialize your variables here). viewDidLoad is called when the view is first loaded from the nib (if you're not using a nib then use loadView), so set up your UI widgets here.

viewWillAppear is called (I believe) every time you re-display the viewController, so initializing things there will indeed cause weird behavior.

Docs: https://developer.apple.com/iphone/...ef/occ/instm/UIViewController/viewWillAppear:
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
If your app gets a memory warning the view controllers that are not currently visible will unload their view and set it to nil. When those view controllers are shown again they will load their view and call their subclasses' viewDidLoad or loadView methods.

You need to write your code with this in mind so there are no memory leaks or other bugs if your view is unloaded.
 

Labeno

macrumors 6502
Original poster
Jul 21, 2008
351
1,089
If your app gets a memory warning the view controllers that are not currently visible will unload their view and set it to nil. When those view controllers are shown again they will load their view and call their subclasses' viewDidLoad or loadView methods.

You need to write your code with this in mind so there are no memory leaks or other bugs if your view is unloaded.

This makes a lot of sense. Thank you very much for the information!!!

Can the memory warning be avoided? If so, is it a bad practice to let views remain loaded when the system is asking you to free memory?

Also, does this somehow relate to controllers taking a long time to finally show up (sometimes 4 or 5 seconds). E.g. a controller used to enter someones name using a keyboard?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
The memory warning happens if your app uses too much memory, for some definition of too much memory. If you don't free up memory the OS will kill your app.

The memory warning comes through didReceiveMemoryWarning and a similar method on your app delegate. If you don't call the base class in your derived class then I assume the base class won't release the view. However, your app could be killed in that case. I would recommend saving any state in didReceiveMemoryWarning, calling the base class, and in viewDidLoad restore the state.

I don't know why a controller would take a long time to load unless something it needs has been completely unloaded.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.