stuart-d2
3/3/2015 - 11:45 AM

Setting Up A Root View Controller Programmatically

Setting Up A Root View Controller Programmatically

//Creating the view hierarchy and passing in a view -- *ViewController.m
#import "BNRHypnosisViewController.h" // the view controller
#import "BNRHypnosisView.h" // the view

@implementation BNRHypnosisViewController
//overriding the UIViewController method loadView to create view hierarchy
//Create the view
- (void)loadView
{
//taking the view -- BNRHypnosisview and init with a frame
CGRect frame = [UIScreen mainScreen].bounds;
BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:frame];

// Set it as *the* view of this view controller
self.view = backgroundView;
}

@end
// Creating the view controller | *ViewController.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BNRHypnosisViewController : UIViewController


@end
//Setting a root view controller | *AppDelegate.m
#import "AppDelegate.h"
#import "BNRHypnosisViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    //Creating a instance of the VC
    BNRHypnosisViewController *hvc = [[BNRHypnosisViewController alloc] init];

    //setting the root view controller
    self.window.rootViewController = hvc;
    return YES;
}