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

时间:2017-01-02 08:38:25

标签: ios uinavigationcontroller swift2 uitabbarcontroller viewcontroller

我有tabBarController个标签。每个标签内都有一个navigationController。每个navigationController都有3个孩子viewControllers

                           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的子控制器。

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
  }
}

2 个答案:

答案 0 :(得分:1)

请您更新 buttonAction:

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

答案 1 :(得分:0)

首先,您无法呈现setViewControllers,因为它已经是tabBarController的一部分。

调用viewcontroller时,它会完全替换navigationViewController的{​​{1}}堆栈,因此您还需要在ViewPurple数组中添加numVCs并设置{ {1}}作为您的根视图控制器。

然后,在调用ViewPurple时,将返回popToRootViewController