Ted
Ted

Reputation: 3885

ViewController programmatically?

I would like to know what steps are needed once a fresh "Single view" project was created in xcode, in order to achieve: 1. a viewController that initializes without a NIB, but rather programmatically loads it's own controls in its view.
2. How to get that viewcontroller's view to load and call viewDidLoad?
3. make the view for that controller visible on the screen with all of the controls.

How do I go about this from this function:

-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions 

I am trying to modify a new xcode project but all I get is a black screeen, viewDidLoad doesn't get called

Upvotes: 1

Views: 427

Answers (4)

Alok
Alok

Reputation: 25978

Try This :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    [self.window setRootViewController:homeVC];
    [self.window makeKeyAndVisible];
    return YES;
}

Remove Main(storyboard reference) from Main interface of general Setting : enter image description here

Add Launch Image : And select iOS-7 and later in your left corner setting enter image description here

Upvotes: 0

wattson12
wattson12

Reputation: 11174

You need to create a subclass of UIViewController, and setup your view hierarchy either in loadView, or viewDidLoad (depending on the level of customisation)

By subclassing UIViewController the loading method calls will be made for you so you don't have to worry about getting getting viewDidLoad etc.

To make it visible on the screen the simplest way is to set it as the rootViewController of the apps window

inside didFinishLaunchingWithOptions: in your app delegate

self.window.rootViewController = [[MyViewControllerSubclass alloc] init];

Upvotes: 1

Zhang
Zhang

Reputation: 11607

That's your app delegate's application loading method.

In there, you would probably want to create an instance of your custom view controller and assign that as the rootViewController to your app delegate didFinishLoading. There should be a line like:

// app delegate .h file

#import "CustomViewController.h"

@interface
{
    ...
    CustomViewController *myCustomVC;
    ...
}

@property (nonatomic, retain) CustomViewController *myCustomVC;


// app delegate .m file
@implementation AppDelegate

@synthesize myCustomVC;

-(BOOL)application:(UIApplication*)application didFinishLoadingWithOptions:(NSDictionary *)launchOptions 
{
    ...
    myCustomerVC = [[CustomViewController alloc] init];

    [self.window setRootViewController:myCustomVC]; 
    ...
}

Then inside your custom view controller's viewDidLoad method, you can do this as a test:

// custom view controller .m file
-(void)viewDidLoad
{
    self.view.backgroundColor = [UIColor redColor];
}

Upvotes: 1

ekinsol
ekinsol

Reputation: 531

UIViewController *myViewController = [[UIViewController alloc] init];
[myViewController.view setFrame:self.view.bounds];
[self.view addSubview:myViewController.view]; // if you want to add it in another viewcontroller

// For testing, set the background color to something other than white (default)

[myViewController.view setBackgroundColor:[UIColor greenColor]];

And off you go !

Upvotes: 1

Related Questions