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

Question

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

Answers 2
Added at 2017-01-02 09:01
Tags
Question

I have a tabBarController with 2 tabs. Inside each tab there is a navigationController. Each navigationController has 3 child viewControllers

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

I have a button inside ViewPurple and when pressed I want to push on ViewOne,ViewTwo, then ViewThree.

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

Thus from ViewThree I would push back to ViewPurple- This is what I'm trying to achieve

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

Inside ViewPurple I reset ColorNavController's to NumNavController's child controllers.

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

The problem I'm having is once I push on ViewOne and press the back backButton instead of going to ViewPurple I end up on ViewTwo. It's caught in weird cycle where it seems it does't know where the beginning viewController is anymore.

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

Where is the problem at and how do I get back to ViewPurple?

ViewPurple: (first way - no crash but weird cycle)

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

I also tried a second way to present the NumNavController but I kept getting an exception 'Application tried to present modally an active controller. I made a weak ref to ViewPurple and tried to deinit it but it still crashed

ViewPurple: (second way - crash)

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
  }
}
Answers
nr: #1 dodano: 2017-01-02 10:01

Firstly you cannot present the NumNavController as that is already the part of tabBarController.

when setViewControllers is called it completely replace the viewcontroller stack of the navigationViewController so you need to add ViewPurple also in the numVCs array and set ViewPurple as your root view controller.

So then, on calling popToRootViewController will move back to ViewPurple.

nr: #2 dodano: 2017-01-02 11:01

Could you please update your buttonAction:

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