1

我有一个tabBarController与2个选项卡。每个标签内都有一个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并试图DEINIT它,但它仍然坠毁

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

为什么要一次插入三视图控制器。你很好地解释你的问题,但对我来说它不是100%可以理解的。 :( –

+0

@ Dheeraj D你是什么意思?我想从PurpleVC去到一,二,三,我需要走过所有三条。你是说我没必要那么做吗? –

+0

Create push segue从紫色视图到ViewOne或者从紫色视图创建模态segue到'NumNavController'。 –

回答

1

能否请更新您的buttonAction:

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

首先你可以不存在,NumNavController因为这是已经tabBarController的一部分。

setViewControllers被称之为完全替代viewcontroller堆栈navigationViewController,所以你需要添加ViewPurplenumVCs数组中,并设置ViewPurple为您的根视图控制器。

那么,在致电popToRootViewController将回到ViewPurple

+0

我从一开始就试过。什么都没发生。我没有包括它。它永远不会“弹出” –

相关问题