简体   繁体   English

从另一个UIViewController内部呈现UIViewController?

[英]Presenting a UIViewController from inside another UIViewController?

I'm pretty new to iOS, and am attempting to present a view controller from within another view controller as described here: 我是iOS的新手,正尝试从另一个视图控制器中呈现一个视图控制器,如下所述:

Stack overflow answer (see third answer) 堆栈溢出答案 (请参阅第三个答案)

and also here on Apple's site 也在苹果网站上

I start with the Hello World Example code . 我从Hello World Example代码开始 I open up MyViewController.m and modify the updateString() method, adding this code to the end: 我打开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
    }
}

I have also created a NIB file called "HighScore.xib" that I put with the other NIB files that came with the HelloWorld, and I also added HighScore.xib to the XCode project, and dragged it into the "Copy Bundle Resources" section. 我还创建了一个名为“ HighScore.xib”的NIB文件,并将其与HelloWorld随附的其他NIB文件放在一起,并将HighScore.xib添加到了XCode项目中,并将其拖到“复制捆绑资源”部分中。 。

As-is, I get an error when running and entering text into the edit field: 照原样,在运行并将文本输入编辑字段时出现错误:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'HighScore'' *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在名称为'HighScore'的捆绑软件:'NSBundle(已加载)'中加载NIB

However if I change the final statement to use "rootViewController" instead of "self" then nothing happens. 但是,如果我将最终语句更改为使用“ rootViewController”而不是“ self”,则不会发生任何事情。 No exception, but no new view is presented either. 也不例外,但也没有提供新视图。

Questions: 问题:

1) Why won't my NIB load? 1)为什么我的NIB无法加载? I'm obviously doing something wrong here. 我显然在这里做错了。

2) Why does using "rootViewController" instead of "self" produce different results? 2)为什么使用“ rootViewController”而不是“ self”会产生不同的结果? Shouldn't the rootViewController find the same class as "self"? rootViewController是否应该找到与“ self”相同的类?

Note that when using "rootViewController" I get a warning in XCode: "MyViewController' may not respond to '-presentViewController:animated:completion" 请注意,使用“ rootViewController”时,我在XCode中收到警告:“ MyViewController”可能不会响应“ -presentViewController:animated:completion”

I solved this problem by: 我通过以下方法解决了这个问题:

a) Using the tutorial here: a)在这里使用教程:

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

b) Right-clicking and doing "Get Info" on the .xib file in the XCode project, and modifying its Type to be "file.xib" instead of "sourcecode.xib". b)右键单击XCode项目中的.xib文件并对其进行“获取信息”,然后将其“类型”修改为“ file.xib”而不是“ sourcecode.xib”。 Without this second change, it would not load ! 如果没有第二次更改,它将无法加载 Bizarre. 奇怪 But this was ultimately the problem!! 但这最终是问题所在!!

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

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