【问题标题】:App opens to specific ViewController inside Navigation Controller应用程序打开到导航控制器内的特定 ViewController
【发布时间】: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")

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

【问题讨论】:

    标签: xcode swift uinavigationcontroller appdelegate


    【解决方案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];
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      相关资源
      最近更新 更多