Order of UIViewController initialization and loading?

The view loading system on the iPhone works like this.

Up vote 8 down vote favorite 8 share g+ share fb share tw.

I'm fairly new to UI programming on the Mac and iPhone, and I've run across something that somewhat puzzles me. A UIViewController has 3 methods that involve the initialization of it and its view: init (and init-like methods) loadView viewDidLoad (delegate method) I'd expect these to occur in the order above. First UIViewController is alloc'ed by some other object, then init is immediately called (or some other init method, like initWithStyle).

Only once the object is initialized would I expect it to call its own loadView function, after which the view, once loaded, calls the viewDidLoad delegate method. This doesn't happen, for instance: @implementation UIViewControllerSubclass - (id)init { NSLog(@"0"); if (self = super init) { NSLog(@"1"); } return self; } - (void)loadView { super loadView; NSLog(@"2"); } - (void)viewDidLoad { super viewDidLoad; NSLog(@"3"); } @end Produces the console output: 0 2 3 1 The loadView and viewDidLoad methods, therefore, cannot make delegate calls, as the delegate is usually set after the call to super init, which (as shown above) is called after loadView and viewDidLoad have run: UIViewControllerSubClass *someViewController = UIViewControllerSubclass alloc init; viewController setDelegate:self; If I want to run code that sets up the ViewController in some way, notifying the delegate as it goes, should the code reside in the init method? Isn't the reason for loadView existing to allow such code to be run at the appropriate moment?

It looks to me like I'll have to create a new initWithDelegate method which sets the delegate ivar before calling super init, is this right, or am I going about this the wrong way? Thanks in advance :) iphone objective-c cocoa-touch uiviewcontroller link|improve this question edited Jan 13 '10 at 2:54Peter Hosey53.4k445137 asked Jan 13 '10 at 0:21Dan11829 25% accept rate.

The view loading system on the iPhone works like this: When you initialize a view controller (either with -init or -initWithNibName:bundle:), it doesn't actually create and initialize the view. When you call -view for the first time, it calls -loadView. By default, -loadView just loads the view from the xib file (nibName).

If you override this, though, you're responsible for creating the view and assigning it to the view controller's view property. As an example: - (void)loadView { UIView *view = UIView alloc initWithFrame:UIScreen mainScreen applicationFrame; // add subviews self. View = view; view release; } Every time you create the view, which is different from the view becoming visible and showing onscreen, it calls -viewDidLoad.

(-viewDidAppear/-viewDidDisappear is for the visibility of the view on-screen) Since we're already off-track, let's consider memory management. When the view is offscreen, the system will automatically set the view property of a view controller to nil. The problem is that all the subviews of that view are leaking.

How so? Well, the retain count for each subview is 2 (views retain subviews, and your view controller has an outlet/ivar to it). When the view is nil, the retain count of that view is 1.

It doesn't make sense for a view to stick around if a view isn't showing, so you set it to nil in -viewDidUnload (which is a hook for whenever the view is set to nil).

The initWithNibName:bundle: method is the designated initializer for the UIViewController class. Try overriding and using it instead of init: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = super initWithNibName:nibNameOrNil bundle:nibBundleOrNil) { } return self; } ... UIViewControllerSubClass *someViewController = UIViewControllerSubclass alloc initWithNibName:@"UIViewControllerSubclass" bundle:nil.

Gerry3 is right. This stuff still confuses me too. Check out the docs on designated initializers: developer.apple.com/iphone/library/docum... Also note that if your controller is created by a nib being loaded then only initWithCoder will get called.

LoadView doesn't get called in that case either. Because of this it seems like most of the code I've seen does most initialization in stuff like viewDidLoad even though that seems wrong, but it seems to be the best method that gets called in both cases where something is loaded in a nib and created programmatically. But the reason this seems out of order is that the super init is calling loadView etc. –.

Most of the code I've seen does most initialization in stuff like viewDidLoad even though that seems wrong" Actually it is wrong. The reason for that is that your view might be unloaded while your viewcontroller would still be around, ready to load again the view on demand. You therefore risk to re-initialize your variables and in some cases that might lead to hard-to-track logical problems in your app.

– KPM Mar 13 at 21:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions