Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loadView of UIViewController not called

I want to setup a UIViewController within a NavigationController programmatically, however the loadView nor viewDidLoad method get called.

This is my code in the app delegate:

MyViewController *viewController = [[MyViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:window.frame];
viewController.view = view;

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

When I start the app I see a navigationbar, but no calls to loadView. What am I missing? I thought loadView gets called after you call view

Edit

MyViewController *viewController = [[MyViewController alloc] init];
[viewController view];  // doesn't look right?

UINavgationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:[navController view];
[window makeKeyAndVisible];

edited towards Jonah's comment, but loadView still doesn't get called.

like image 855
L. Steiner Avatar asked Sep 11 '11 19:09

L. Steiner


People also ask

Is loadView called before viewDidLoad?

Use viewDidLoad( ), which is called AFTER loadView( ) has completed its job and the UIView is ready to be displayed. viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called.

When loadView is called?

The view controller calls this method when its view property is requested but is currently nil . This method loads or creates a view and assigns it to the view property. If the view controller has an associated nib file, this method loads the view from the nib file.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

Which of the following method is called after UIViewController is initialized?

A UIViewController has 3 methods that involve the initialization of it and its view: init (and init-like methods) loadView. viewDidLoad (delegate method)


3 Answers

A UIViewController will create its view (by loading it from a nib or implementing -loadView) when the controller's view getter is called and its view is currently nil.

In the code shown you never invoke the view property's getter, only its setter.

Also, you are assigning the controller's view from your app delegate. UIViewControllers are expected to create their own views on demand, not have them provided by some other class. This approach will cause you problems later when you realize that the controller unloads its view and attempts to recreate it in response to memory warnings. Let your controller create its view, don't try to pass it one.

like image 133
Jonah Avatar answered Oct 04 '22 20:10

Jonah


maybe you were not facing this issue... but the other day I ran into the same irritating trouble.. loadView, viewDidLoad and viewWillAppear not being called in my UIViewController.

My issue was v. simple but bit tricky to catch if you are not very careful. Instead of writing

-(void) loadView

I wrote:

-(void) loadview

Please note that this won't fire any warning. The difference of "V" and "v" in loadView can easily be missed. And obviously, since loadView didn't get called, viewDidLoad/viewWillAppear won't get called either as there was no view that got loaded (am not using any nib..creating the view programmatically).

-Anshu

like image 26
Himanshu Singh Avatar answered Oct 04 '22 20:10

Himanshu Singh


Another gotcha worth noting is if you define a

@synthesize view;

without a matching @property in your implementation, this can result in calls to your view controller's returning nil, and no call to your loadView method.

like image 35
John Dunne Avatar answered Oct 04 '22 21:10

John Dunne