开发者

How to present a standard UIViewController modally

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://d开发者_Python百科eveloper.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!


Where do I configure my WelcomeViewController's view?

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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜