Pippo
Pippo

Reputation: 1473

Pass data from ViewController to Viewcontroller that is now part of TabBarController

so i have 2 view controllers that have now become parts of a TabBarController. I was just passing a variable "selectedPack" from vc A to vc B by:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let transportJourneyViewController = storyBoard.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController
        transportJourneyViewController.selectedPack = self.packArray[indexPath.item].packID
self.present(transportJourneyViewController, animated:true, completion:nil)

however now that vc B or JourneyViewController is part of the TabBarController i need to change to it with something like:

let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
    tbc.selectedIndex = 1
self.present(tbc, animated: true, completion: nil)

problem I'm having is trying to pass data tot he variable "selectedPack" as shown in the first code block, because this variable doesn't exist in "tbc.selectedIndex = 1" it exists in "JourneyViewController". Im getting confused because I'm thinking that even though it is part of a tabbarcontroller, when you change, you are changing to that viewcontroller which obviously isn't the case, even though that is what you see.

Upvotes: 2

Views: 73

Answers (1)

Abhishek Sharma
Abhishek Sharma

Reputation: 3283

You can try with below way

let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
        tbc.selectedIndex = 1

        // Suppose your viewcontroller is at tab bar first index.
        let transportJourneyViewControllerOBJ = tbc.viewControllers?[0] as! ViewController
        transportJourneyViewControllerOBJ.strValue = "Testvalue"
        self.present(tbc, animated: true, completion: nil)

Upvotes: 2

Related Questions