如何从非初始 NavigationController 场景开始

How to start from a non-initial NavigationController scene

我正在尝试模仿 Apple 默认应用程序中的导航逻辑 "Photos"。下面是应用导航逻辑的说明。

当您第一次启动该应用程序时,您会进入 "Moments" 视图,该视图位于应用程序的第三高层次。这非常有趣,因为整个 "Photos" 选项卡似乎嵌入到单个 NavigationController 中。

但是,如果所有场景都嵌入到单个 NavigationController 中,如何从非初始场景开始?

这是我的 hackish 实现,没有将场景嵌入到任何 NavigationController 中(所有 segues 都显示为 show

结果是我想要的行为,但我不确定内存问题。我是否在每个循环中分配新的 VC?而且故事板看起来不对。

作为答案,一个简单的 Storyboard 截图和简短的解释会很好。

正如我在评论中所说,您需要修改 UINavigationController.viewControllers 属性 您可以使用 setViewControllers(_:animated:) 方法或直接修改 [=13] =] 属性 其中 rootViewController 将是 viewControllersArray[0]topViewController 将是 viewControllersArray[count-1]

此 属性 描述和详细信息可以在 UINavigationController 文档中找到

viewControllers

Property

The view controllers currently on the navigation stack.

声明 SWIFT

var viewControllers: [UIViewController]

Discussion The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to false.

例子

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
    let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
    let thirdViewController = storyboard.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController

    self.navigationController?.setViewControllers([firstViewController,secondViewController,thirdViewController], animated: false)