How to present a standard UIViewController modally

1.8k views Asked by At

I'm trying to present a standard ViewController modally, but can't figure out how to do it. The view controller will have buttons which will ultimately trigger the dismissing actions, so I don't need to wrap it in a NavigationController. Also, I'm doing all of this programmatically, without .xibs.

Here's the code I'm using:

- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"view did appear within rootviewcontroller");
    WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
    [self presentModalViewController:welcome animated:true];
    [welcome release];
}

The problem is that I haven't set the WelcomeViewController's view, so loadView is not getting run, which means no content is being drawn to the screen.

Every example I find, including Apple's, uses either a .xib to initialize the ViewController, a NavigationController that adds a RootViewController, or both. My understanding is that loadView is called automatically for you in both of these scenarios. http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3

Where do I configure my WelcomeViewController's view? Right there after the alloc/init? Inside WelcomeViewController's init method?

Thanks!

2

There are 2 answers

4
Jim On BEST ANSWER

Where do I configure my WelcomeViewController's view?

Override the loadView method in your subclass. See View Controller Programming Guide for iOS.

4
Rog On

Here's a simple example of how you can go about it without using NIBs:

In your AppDelegate didFinishLaunchingWithOptions:, you create an instance of your custom view controller and add it as a subview of window (pretty standard).

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

When creating the vc instance, you use the designated initialiser which will be called on the new instance of the view controller. You don't specify any nibs because you will do your custom initialisation inside the method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor orangeColor]];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setNumberOfLines:2];
        [label setText:@"This is the vc view with an\norange background and a label"];
        [label setTextColor:[UIColor whiteColor]];
        [label setTextAlignment:UITextAlignmentCenter];
        [self.view addSubview:label];
        [label release];
    }
    return self;
}