繁体   English   中英

从另一个UIViewController内部呈现UIViewController?

[英]Presenting a UIViewController from inside another UIViewController?

我是iOS的新手,正尝试从另一个视图控制器中呈现一个视图控制器,如下所述:

堆栈溢出答案 (请参阅第三个答案)

也在苹果网站上

我从Hello World Example代码开始 我打开MyViewController.m并修改updateString()方法,并将此代码添加到末尾:

{
        // Get the application's "window"
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
        // Get the window's rootViewController (NOTE: Only works on iOS 4.0 or later!)
    UIViewController *rootViewController = window.rootViewController;

        // Create our new controller
    UIViewController *enterNameController = [[UIViewController alloc] initWithNibName:@"HighScore" bundle:[NSBundle mainBundle]];

    if (enterNameController != NULL)
    {
        printf("\nLoaded UIView controller. Installing as actionSheetController...\n\n");

        UINavigationController *navigationController = [[UINavigationController alloc]
                         initWithRootViewController:enterNameController];

            // Present out Enter Name for High Score view as a modal view
    //  [rootViewController presentViewController:navigationController animated:YES];       // iOS 2.0 and newer
        [self presentViewController:navigationController animated:YES completion: nil];     // iOS 5.0 and newer
    }
}

我还创建了一个名为“ HighScore.xib”的NIB文件,并将其与HelloWorld随附的其他NIB文件放在一起,并将HighScore.xib添加到了XCode项目中,并将其拖到“复制捆绑资源”部分中。 。

照原样,在运行并将文本输入编辑字段时出现错误:

*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在名称为'HighScore'的捆绑软件:'NSBundle(已加载)'中加载NIB

但是,如果我将最终语句更改为使用“ rootViewController”而不是“ self”,则不会发生任何事情。 也不例外,但也没有提供新视图。

问题:

1)为什么我的NIB无法加载? 我显然在这里做错了。

2)为什么使用“ rootViewController”而不是“ self”会产生不同的结果? rootViewController是否应该找到与“ self”相同的类?

请注意,使用“ rootViewController”时,我在XCode中收到警告:“ MyViewController”可能不会响应“ -presentViewController:animated:completion”

我通过以下方法解决了这个问题:

a)在这里使用教程:

http://timneill.net/2010/09/modal-view-controller-example-part-1/

b)右键单击XCode项目中的.xib文件并对其进行“获取信息”,然后将其“类型”修改为“ file.xib”而不是“ sourcecode.xib”。 如果没有第二次更改,它将无法加载 奇怪 但这最终是问题所在!!

暂无
暂无

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

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