objective-c 从 UIViewController 加载 UINavigationController 作为子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/811478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:28:18  来源:igfitidea点击:

Load a UINavigationController as a subview from UIViewController

iphoneobjective-cuiviewuinavigationcontrollersubview

提问by XcoderMi2

I am a newbi in iPhone development.I want to build an app which will have a UIViewController first , which will have a button.Now on clicking the button, it shud load a UINavigation controller. Here is how i m approaching :

我是 iPhone 开发的新手。我想构建一个应用程序,它首先有一个 UIViewController,它将有一个按钮。现在点击按钮,它会加载一个 UINavigation 控制器。这是我如何接近:

  1. i created a UIViewController class, where i took a

    -(IBAction) PressMeFunc:(id) sender
    

    for the button to be pressed.

  2. Then i created a UIView xib file.I did the required steps in the IB.

  3. Then in the AppDelegate, i added the ViewController's instance as a Subview of the window.

  1. 我创建了一个 UIViewController 类,在那里我做了一个

    -(IBAction) PressMeFunc:(id) sender
    

    按钮被按下。

  2. 然后我创建了一个 UIView xib 文件。我在 IB 中做了所需的步骤。

  3. 然后在 AppDelegate 中,我添加了 ViewController 的实例作为窗口的子视图。

Upto this it is OK.

到此为止就OK了。

Next, how do i load a navigationcontroller on the press of the button?

接下来,如何在按下按钮时加载导航控制器?

I know how to build a navigationController project from window-based app, but i am having a tough time doing NavigationController as a subview of UIView.

我知道如何从基于窗口的应用程序构建一个 navigationController 项目,但是我很难将 NavigationController 作为 UIView 的子视图。

Your help is much appreciated.

非常感谢您的帮助。

回答by Mark Bessey

The NavigationController is designed to take over the screen when it's used, so you have to decide how to manage the transition to the NavigationController from your original ViewController. You can do this with presentModalViewController, or by handling removing your original view and swapping the NavigationController in programatically.

NavigationController 旨在在使用时接管屏幕,因此您必须决定如何管理从原始 ViewController 到 NavigationController 的过渡。您可以使用 presentModalViewController 或通过处理删除原始视图并以编程方式交换 NavigationController 来执行此操作。

Here's the Apple documentationfor setting up a NavigationController programatically.

这是用于以编程方式设置 NavigationController的 Apple 文档

The code is going to look something like this (from Apple's doc):

代码看起来像这样(来自 Apple 的文档):

GroupsController *groupsController = [[[GroupsController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:groupsController];

Now, once you've created the NavigationController, and added its first viewcontroller to it, you need to transition to it. You can do that with CATransitions, or with

现在,一旦您创建了 NavigationController,并向其添加了第一个视图控制器,您就需要过渡到它。您可以使用 CATransitions 或使用

[myViewController presentModalViewController: navigationController];

回答by Zaky German

You shouldn't add a UIViewController's (including navigation) view as a subview to a view managed by another view controller.

您不应将 UIViewController 的(包括导航)视图作为子视图添加到由另一个视图控制器管理的视图中。

Here's a relevant read: http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/

这是相关阅读:http: //blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/

In your case, what you can do is remove the viewcontroller's view from the window, then add the navigation controller's view to the window.

在您的情况下,您可以做的是从window 中删除 viewcontroller 的 view ,然后将导航控制器的 view 添加到 window 。

[viewController.view removeFromSuperview];
[window addSubview:navigationController.view];

You can also add it as a modal view controller as was suggested here, or you can make the first view controller a navigation controller, and onto that navigation controller's stack, push the 2nd navigation controller:

您还可以按照此处的建议将其添加为模态视图控制器,或者您可以将第一个视图控制器设为导航控制器,然后将第二个导航控制器推送到该导航控制器的堆栈上:

[navigationController pushViewController:secondNavigation animated:NO];

Edit: heh just noticed I'm answering a 09 question

编辑:嘿刚刚注意到我在回答 09 问题

Edit #2: This may be irrelevant to iOS 5 and the UIViewController containment thing they've added, still hadn't a chance to check it out, but if you're reading this answer, you might wanna.

编辑 #2:这可能与 iOS 5 和他们添加的 UIViewController 包含内容无关,仍然没有机会检查它,但是如果您正在阅读这个答案,您可能想要。