简体   繁体   中英

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

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

That property is an array of UIViewController s, 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.

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)

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM