Data transfer between ViewController and TabBarController

I have a couple of questions. How to transfer data (which I received after Alamofire request) to one of the TabBarController child elements?

The first problem I have is that I cannot override Func prepareForSegue inside the input in action (when the button taps), it says that I can only override a member of the class. But if I put func outside of IBAction, then I will not send the data I need.

And the second problem is that when I put the overridden function outside of IBAction, and the code looks like this:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

            let homeVC = segue.destinationViewController as HomeViewController
            homeVC.templateForCell = templates
        }

when i run it i got an error:

Failed to set value of type 'UITabBarController' for HomeViewController

(HomeViewController is my target view, where I should transfer data from Alamofire).

+4
2

prepareForSegue. , ViewController ViewController ViewBarController, , .

let vc = self.tabBarController.viewControllers![1] as! HomeViewController
vc.templateForCell = templates

ViewControllers TabBar , :

let navController = self.tabBarController.viewControllers![1] as! UINavigationController
let vc = navController.topViewController as! HomeViewController
vc.templateForCell = templates
+6

Xcode 8, Swift 3.x - . , . startWizard true .

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "segueFromNewUserToDashboard") {
        let dashboardController = segue.destination as! MyTabBarController
        for viewController in dashboardController.viewControllers! {
            let navViewController = (viewController as! MyNavigationController).topViewController!
            if (navViewController.isKind(of: DashboardViewController.self) == true) {
                (navViewController as! DashboardViewController).startWizard = true
                break
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1654597/


All Articles