0

我正在尝试以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视图?就在分配/初始化之后?在WelcomeViewController'sinit方法里面?

谢谢!

4

2 回答 2

3

我在哪里配置 WelcomeViewController 的视图?

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

于 2011-02-18T20:35:21.627 回答
1

这是一个简单的示例,说明如何在不使用 NIB 的情况下进行操作:

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

- (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实例时,您使用指定的初始化程序,它将在视图控制器的新实例上调用。您没有指定任何 nib,因为您将在方法内进行自定义初始化:

- (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;
}
于 2011-02-18T20:55:06.897 回答