应用在导航控制器中打开特定 ViewController
App opens to specific ViewController inside Navigation Controller
我正在尝试让我的应用对特定 ViewController 开放,该 ViewController 深深嵌入导航控制器中,但不是根 ViewController。我在应用程序委托 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 但是顶部没有导航栏需要在应用程序中导航。
要从 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
。如果您仍然遇到问题,请告诉我。谢谢。
这里是 Objective-C 版本打开一个特定的 UIViewController
深深地嵌入在 UINavigationController
(不是 rootController)中。
如果我们在评论中以@bdc为例,我们想打开UIViewController
D:
- A : 根视图控制器
- N : 导航控制器
- B、C、D:嵌入在 N 中的 UIViewControllers
- A -> N[B C D]
在 InterfaceBuilder 中为通过 D 的层次结构中的每个 UIViewController
或 UINavigationController
设置 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 开放,该 ViewController 深深嵌入导航控制器中,但不是根 ViewController。我在应用程序委托 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 但是顶部没有导航栏需要在应用程序中导航。
要从 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
。如果您仍然遇到问题,请告诉我。谢谢。
这里是 Objective-C 版本打开一个特定的 UIViewController
深深地嵌入在 UINavigationController
(不是 rootController)中。
如果我们在评论中以@bdc为例,我们想打开UIViewController
D:
- A : 根视图控制器
- N : 导航控制器
- B、C、D:嵌入在 N 中的 UIViewControllers
- A -> N[B C D]
在 InterfaceBuilder 中为通过 D 的层次结构中的每个 UIViewController
或 UINavigationController
设置 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];
希望这会有所帮助。