umut
umut

Reputation: 45

pass data from tabbar controller to view controller in swift and xcode

I want to pass data from a view controller to tabbar controller, and then from this tab bar controller to its view controller with using their class. I succeeded to transfer from view controller to tabbar controller using segue. However, I cannot transfer data from tabbar controller to its one of the view controller.

Any idea/documentation will be appreciated. Here is the screenshot about what I want to do from xcode screenshot-xcode

Upvotes: 0

Views: 3535

Answers (2)

umut
umut

Reputation: 45

At last, I am able to solve the issue, many thanks to the answer. Here is the detailed explanation for beginners like me:

This is the source controller class, which is a tabbar controller and it transfers the data:

class SourceTC: UITabBarController {

    var dataTransferFrom = "transfer this string"

    override func viewDidLoad() {
        super.viewDidLoad()

        let finalVC = self.viewControllers![0] as! DestinationVC //first view controller in the tabbar
        finalVC.dataTransferTo = dataTransferFrom

    }

}

and this is the destination controller class, which is a view controller under tabbar controller and it gets the transferred data:

class DestinationVC: UIViewController {

    var dataTransferTo = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        print(dataTransferTo)

    }
}

Upvotes: 2

Marco Boschi
Marco Boschi

Reputation: 2333

Take a look at the documentation for the tab bar controller, in particular the viewControllers property.

That property is an array of UIViewControllers, in the order they appear in the tab bar, so you can pick the one you need (viewControllers[0] from your screen shot), cast it to your specific view controller subclass and then pass it your data.

Upvotes: 2

Related Questions