John Doe
John Doe

Reputation: 43

How to programmatically load UIViewController

I have the following code and I want to load the UIViewController. How can I initialize and load the UIViewController.

- (void) applicationDidFinishLaunching:(UIApplication*)application
{

    CC_DIRECTOR_INIT();

    NSLog(@"applicationDidFinishLaunching");

    MainViewController *controller = [[MainViewController alloc] init]; 





}

Upvotes: 4

Views: 11898

Answers (5)

Septiadi Agus
Septiadi Agus

Reputation: 1775

[viewController view]

That's how to load viewController. When the view is accessed it's lazy loaded.

Upvotes: 0

Javier Fernández
Javier Fernández

Reputation: 41

TestViewController *testController = [[TestViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:testController];
[self.window makeKeyAndVisible];

Although adding a subview will work fine, you will receive the following warning unless you set your controller as RootViewController:

Application windows are expected to have a root view controller at the end of application launch

Upvotes: 4

codefinger
codefinger

Reputation: 10318

From your delegate you can do this (assuming you have IBOutlet UIWindow *window):

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

Once a controller is loaded, you can push others (from the UIViewController):

controller = [[MainViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];

Here is a link to the documentation for UINavigationController.pushViewController

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:

Upvotes: 4

GendoIkari
GendoIkari

Reputation: 11914

Are you using a nib file to set up the user interface of your view? The code you currently have does load and initialize the ViewController. But you would then need to add some user interface elements to your view, and present that view controller in your application. If you arre using a nib file for your user interface, then you want:

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"nibFileName" bundle:nil];

This will associate your controller with the nib file. If you are not using a nib file, you need to programmatically add each element that you wish to display.

After your view is set up, you then need to present the view controller, by either adding it as a subview to your current view, or using a navigationController to push the new viewController. You need to be more specific about exactly what you are trying to do.

Upvotes: 2

Tommy
Tommy

Reputation: 100612

I think what you want to add is:

[[NSBundle mainBundle] loadNibNamed:@"nibWithMainViewControllerAsOwner" owner:controller options:nil];

loadNibNamed:owner:options: is the only method added to NSBundle by UIKit. See NSBundle UIKit Additions Reference. And if there's any problem with outlets not being wired up correctly then check all your outlets are key-value coding compliant (alternative answer: make sure they're correctly exposed as properties).

Upvotes: 0

Related Questions