繁体   English   中英

如何模态呈现标准的UIViewController

[英]How to present a standard UIViewController modally

我试图以模态呈现标准的ViewController ,但无法弄清楚该怎么做。 视图控制器将具有最终将触发取消动作的按钮,因此我不需要将其包装在NavigationController 另外,我正在以编程方式完成所有这些操作,而没有.xibs。

这是我正在使用的代码:

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

问题是我尚未设置WelcomeViewController's视图,因此loadView没有运行,这意味着没有内容绘制到屏幕上。

我发现的每个示例(包括Apple的示例)都使用.xib初始化ViewController,添加RootViewController的NavigationController或两者。 我的理解是在这两种情况下都会自动为您调用loadView。 http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3

在哪里配置我的WelcomeViewController's视图? 在alloc / init之后就在那里吗? WelcomeViewController's init方法内部?

谢谢!

在哪里配置我的WelcomeViewController的视图?

覆盖子类中的loadView方法。 请参阅《适用于iOS的View Controller编程指南》

这是一个简单的示例,说明您无需使用NIB即可如何进行操作:

在您的AppDelegate didFinishLaunchingWithOptions: ,您将创建自定义视图控制器的实例,并将其添加为窗口的子视图(非常标准)。

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

创建vc实例时,使用指定的初始化程序,该初始化程序将在视图控制器的新实例上调用。 您不指定任何笔尖,因为您将在方法中进行自定义初始化:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM