Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a view controller & view hierarchy programmatically in Cocoa Touch without xib

It seems like all of the Cocoa Touch templates are set up to load a nib.

If I want to start a new project that's going to use a view controller, and load its view(hierarchy) programatically, not from a nib/xib, what are the steps to setting that up or adjusting a template.

I though all I had to do was implement -loadView, but I have trouble every time I try to do this.

like image 887
Joe Ricioppo Avatar asked May 01 '09 01:05

Joe Ricioppo


People also ask

How do I load a view view controller?

Add Views to Your View Controller To that root view, you add the custom views you need to present your interface. In storyboards, you add views by dragging them onto the view controller scene. For example, the following figure shows a view controller with an image view and button on an iPhone.

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


2 Answers

It's reasonably simple to do completely programmatic user interface generation. First, you need to edit main.m to look something like the following:

int main(int argc, char *argv[]) 
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];  
    UIApplicationMain(argc, argv, nil, @"MyAppDelegate");
    [pool release];
    return 0;   
}

where MyAppDelegate is the name of your application delegate class. This means that an instance of MyAppDelegate will be created on launch, something that is normally handled by the main Nib file for the application.

Within MyAppDelegate, implement your applicationDidFinishLaunching: method similar to the following:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if (!window) 
    {
        [self release];
        return;
    }
    window.backgroundColor = [UIColor whiteColor];

    rootController = [[MyRootViewController alloc] init];

    [window addSubview:rootController.view];
    [window makeKeyAndVisible];
    [window layoutSubviews];    
}

where MyRootViewController is the view controller for the primary view in your window. This should initialize the main window, and add the view managed by MyRootViewController to it. rootController is kept as an instance variable within the delegate, for later reference.

This should let you programmatically generate your user interface through MyRootViewController.

like image 155
Brad Larson Avatar answered Oct 06 '22 03:10

Brad Larson


UIViews themselves do not have a hierarchy, UINavigationControllers do. So init one of those, and pop a UIViewController onto it's stack. This should do it, in the most basic way possible, with no XIB files at all. You should be able to build on this.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
   UINavigationController *navController = [[UINavigationController alloc] init];

   UIViewController *viewController = [[UIViewController alloc] init];

   // set the properties of viewController here, to make it look like you want

   [navController pushViewController:viewController animated:NO];

   [window addSubview:navController.view];

   // Don't forget memory management
   [navController release];
   [viewController release];

   [window makeKeyAndVisible];
}
like image 23
mmc Avatar answered Oct 06 '22 03:10

mmc