How to programmatically load a UIViewController

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

- (void) applicationDidFinishLaunching:(UIApplication*)application { CC_DIRECTOR_INIT(); NSLog(@"applicationDidFinishLaunching"); MainViewController *controller = [[MainViewController alloc] init]; } 
+4
source share
5 answers

From your delegate, you can do this (if you have an IBOutlet UIWindow * window):

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

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

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

Here is the documentation link for UINavigationController.pushViewController

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

+4
source
 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 if you do not set your controller as a RootViewController:

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

+4
source

Do you use the nib file to customize the user interface of your view? The code you have really loads and initializes the ViewController. But then you will need to add some user interface elements to your view and present this view controller in your application. If you are using the nib file for your user interface, you want:

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

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

After your view is configured, you need to present the view controller by adding it as a subtask to the current view or using the navigationController to click on the new viewController. You should be more specific about what exactly you are trying to do.

+2
source

I think you want to add:

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

loadNibNamed:owner:options: is the only method added to the NSBundle using UIKit. See NSBundle UIKit Add-ons Help . And if there are problems with the correct wiring of the outlet, check that all your outlets are compatible with key values ​​(alternative answer: make sure that they are correctly displayed as properties).

0
source

[viewController view]

How to load viewController. When access to the view opens, it lazily loads.

0
source

All Articles