如何以模态方式呈现标准 UIViewController

发布于 2024-10-18 09:07:53 字数 1235 浏览 1 评论 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 的 视图?就在 alloc/init 之后吗?在 WelcomeViewController 的 init 方法中?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-10-25 09:07:53

在哪里配置 WelcomeViewController 的视图?

重写子类中的 loadView 方法。请参阅 查看 iOS 控制器编程指南

Where do I configure my WelcomeViewController's view?

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

原野 2024-10-25 09:07:53

下面是一个简单的示例,说明如何在不使用 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文