应用程序打开到导航控制器内的特定 ViewController

Posted

技术标签:

【中文标题】应用程序打开到导航控制器内的特定 ViewController【英文标题】:App opens to specific ViewController inside Navigation Controller 【发布时间】:2016-01-04 06:14:21 【问题描述】:

我试图让我的应用程序打开到一个特定的 ViewController,该 ViewController 深深嵌入在导航控制器中,但不是 RootViewController。我已经尝试在应用程序委托 didFinishLaunchingWithOptions 添加:

  self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
  let storyboard = UIStoryboard(name: "Main", bundle: nil)
  let initialViewController = storyboard.instantiateViewControllerWithIdentifier("NavViewController")
  self.window?.rootViewController = initialViewController
  self.window?.makeKeyAndVisible() 

但这会转到根视图控制器。我尝试将此行更改为:

storyboard.instantiateViewControllerWithIdentifier("MainViewController")

确实打开到正确的视图控制器,但顶部没有导航栏,需要在应用程序中导航。

【问题讨论】:

【参考方案1】:

要从AppDelegate 访问rootViewController,代码如下:

let rootViewController = application.windows[0].rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let notificationVC = mainStoryboard.instantiateViewControllerWithIdentifier("identifier") as! NotificationVC
rootViewController.pushViewController(notificationVC, animated: false)

现在,它将具有navigationBar。让我知道,如果你仍然面临这个问题。谢谢。

【讨论】:

是的,这正是我所需要的。谢谢!现在我正在尝试从未嵌入导航控制器的视图控制器中做类似的事情。有什么想法吗? @bdc 你只是想显示还是想推/弹出? 不管怎样。我想推/弹出,除非那更复杂。 如果要推送/弹出,则需要以编程方式分配UINavigationController。否则你可以通过instantiateViewControllerWithIdentifier方法直接加载任何UIViewController!。 对,但是如果我使用instantiateViewControllerWithIdentifier 跳转到嵌入在导航控制器中的视图控制器(但它不是根 VC),则没有导航栏。如果我也使用 segue,也会发生同样的事情。基本上我有视图控制器 A [B C D] 和 [B C D] 在以 B 为根的导航控制器中。我想从 A 到 C。【参考方案2】:

这里是打开一个特定的 UIViewController 嵌入在 UINavigationController(不是 rootController)中的 Objective-C 版本。

如果我们在 cmets 中以 @bdc 为例,我们要打开 UIViewControllerD:

A:rootViewController N : 导航控制器 B、C、D:嵌入在 N 中的 UIViewControllers A -> N[B C D]

通过 D 为层次结构中的每个 UIViewControllerUINavigationController 在 InterfaceBuilder 中设置 storyboardId

在 AppDelegate 中,代码是直截了当的。您只需实例化所有内容,并使用您的 UINavigationController 推送层次结构中的每个 UIViewController

// Instanciate from storyboard with identifiers

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
UINavigationController * N = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"N"];
UIViewController* B = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"B"];
UIViewController* C = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"C"];
UIViewController* D = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"D"];

// Set up the views hierarchy

self.window.rootViewController = N;
[N pushViewController:B animated:NO];
[N pushViewController:C animated:NO];
[N pushViewController:D animated:YES];
    
    
[self.window makeKeyAndVisible];

希望这会有所帮助。

【讨论】:

以上是关于应用程序打开到导航控制器内的特定 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

选项卡不会以编程方式切换导航控制器内的 tabBar

如何导航到嵌入在标签栏控制器中的视图控制器

检测应用程序是不是从推送通知启动/打开,然后将其重定向到特定视图控制器

自定义 UIView XIB 内的 iOS 导航返回按钮(无导航控制器)

当用户点击推送通知时导航到特定的视图控制器

swift3如何显示导航视图控制器