Creating a view controller without a nib

10,619

Solution 1

Try to use: self.window.rootViewController = controller; instead of [self.window addSubview:controller.view];

Note, that you should also @synthesize window; and create it self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

Solution 2

Instead of initWithNibNamed:, just use alloc and init or any of the other designated initalizers for the view controller. Here is an example from a project

hoverViewController=[[BDHoverViewController alloc] initWithHoverStatusStyle:BDHoverViewStatusActivityProgressStyle];
self.window.rootViewController=hoverViewController;
[self.window makeKeyAndVisible];

also, the correct form( for now anyways) for adding the root view controller to the window in the app delegate is like this:

self.window.rootViewcontroller=controller;
[self.window makeKeyAndVisible];

You don't need to add the view to the window. The above code does it automatically.

Good luck,

T

Share:
10,619
Ben Packard
Author by

Ben Packard

iOS developer and program manager located in Washington, DC.

Updated on June 04, 2022

Comments

  • Ben Packard
    Ben Packard almost 2 years

    In AppDelegate, I want to create a UIViewController subclass and add it's view. The viw itself will be specified in code - there is no nib.

    Based on the apple docs, I should use

    initWithNibName:nil bundle:nil];
    

    and then in loadView of the controller, I add my subviews etc.

    However, the follwing test code below does not work for me. I modelled the AppDelegate code on Apple's PageControl demo, simply because my app will implement a similar structure (specifically a base controller to manage a paged scroll view, and an array of other controller's to build the pages).

    But I suspect my AppDelegate code is the problem, since logging proves that initWithNibName:: and loadView both fire. The app as below runs, but the screen is blank. I am expecting a green view with a label.

    AppDelegate

            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            ScrollerController *controller = [[ScrollerController alloc] initWithNibName:nil bundle:nil];
            [self.window addSubview:controller.view];
            [self.window makeKeyAndVisible];
            return YES;
        }
    

    ScrollerController (the UIViewController subclass)

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)loadView{
        CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
        UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
        contentView.backgroundColor = [UIColor greenColor];
        self.view = contentView;
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 100, 40)];
        [label setText:@"Label created in ScrollerController.loadView"];
        [self.view addSubview:label];
    }