如何将一组新的视图控制器从一个选项卡推送到另一个选项卡?

How to push on new set of view controllers from one tab to another tab?

我有一个带有 2 个标签的 tabBarController。每个选项卡内都有一个 navigationController。每个navigationController有3个childviewControllers

                           tabBarController
                            /            \
                        tabOne          tabTwo
                         /                  \
                  NumNavController     ColorNavController
                       |                      |
                    ViewOne                ViewBlack
                       |                      |
                    ViewTwo                 ViewRed
                       |                      |
                   ViewThree               ViewPurple//(beginning)
                                              |
                                           myButton
                               //myButton pushes on ViewOne>ViewTwo>ViewThree(end)

我在 ViewPurple 中有一个按钮,按下时我想按下 ViewOne、ViewTwo,然后是 ViewThree。

(beginning)ViewPurple > ViewOne > ViewTwo > ViewThree(end)

因此我会从 ViewThree 推回到 ViewPurple- 这就是我想要实现的目标

(beginning)ViewPurple < ViewOne < ViewTwo < ViewThree(end)

在 ViewPurple 内部,我将 ColorNavController 重置为 NumNavController 的 child 控制器。

let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
self.navigationController?.setViewControllers(numVCs, animated: true)
//Apple docs says to set them backwards
//The view controllers to place in the stack. The front-to-back order of the controllers in this array represents the new bottom-to-top order of the controllers in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack

我遇到的问题是,一旦我按下 ViewOne 并按下后退按钮而不是转到 ViewPurple,我最终会进入 ViewTwo。它陷入了一个奇怪的循环,它似乎不知道开始 viewController 在哪里了。

 //I can't push back to ViewPurple
 ViewOne < ViewTwo < ViewOne < ViewTwo < ViewThree(end)

问题出在哪里,如何返回 ViewPurple?

ViewPurple:(第一种方式 - 没有崩溃但奇怪的循环)

class ViewPurple: UIViewController {

@IBAction func myButton(sender: UIButton){
     self.presentNewVCs()
  }

func presentNewVCs(){
     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let viewOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController
     let viewTwoVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoController
     let viewThreeVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewThree") as! ViewThreeController

     let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
     self.navigationController?.setViewControllers(numVCs, animated: true)
  }
}

我也尝试了第二种方式来呈现 NumNavController,但我一直收到异常 'Application tried to present modally an active controller。我对 ViewPurple 进行了弱引用并尝试取消初始化它,但它仍然崩溃了

ViewPurple:(第二种方式 - 崩溃)

class ViewPurple: UIViewController {

weak var parent:ViewPurple!

@IBAction func myButton(sender: UIButton){
     self.presentNewVCs()
  }

func presentNewVCs(){

     let numNavController = self.tabBarController!.viewControllers![0] as! NumNavController

     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
     let viewOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController
     let viewTwoVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoController
     let viewThreeVC = mainStoryboard.instantiateViewControllerWithIdentifier("ViewThree") as! ViewThreeController

     let numVCs = [viewThreeVC, viewTwoVC, viewOneVC]
     numNavController.setViewControllers(numVCs, animated: true)
     self.presentViewController(numNavController, animated: true, completion: nil)
  }

  deinit{
     self.parent = nil
  }
}

首先,您不能显示 NumNavController,因为它已经是 tabBarController 的一部分。

当调用 setViewControllers 时,它会完全替换 navigationViewControllerviewcontroller 堆栈,因此您还需要在 numVCs 数组中添加 ViewPurple 并且将 ViewPurple 设置为您的根视图控制器。

那么,在调用 popToRootViewController 时将移回 ViewPurple

你能更新一下你的 buttonAction:

@IBAction func myButton(sender: UIButton){ 
     let viewOneVC =  mainStoryboard.instantiateViewControllerWithIdentifier("ViewOne") as! ViewOneController 
    self.navigationController.pushViewController(viewOneVC, animate:true) 
}