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

时间:2016-01-04 06:14:21

标签: xcode swift uinavigationcontroller appdelegate

我正在尝试将我的应用程序打开到深深嵌入导航控制器的特定ViewController,但不是RootViewController。我已经尝试在app delegate 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")

并且确实打开正确的viewcontroller,但顶部没有导航栏,需要在应用中导航。

2 个答案:

答案 0 :(得分:1)

要从rootViewController访问AppDelegate,请输入以下代码:

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。如果您仍然面临这个问题,请告诉我。感谢。

答案 1 :(得分:0)

这是Objective-C版本,用于打开一个深入嵌入UIViewController(不是rootController)内部的特定UINavigationController

如果我们在注释中使用@bdc示例,而我们想打开UIViewController D:

  • A:rootViewController
  • N:navigationController
  • B,C,D:N中嵌入的UIViewControllers
  • A-> N [B C D]

通过D为层次结构中的每个storyboardIdUIViewController设置InterfaceBuilder中的UINavigationController

在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];

希望这会有所帮助。