Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data between ViewController and TabBarController

I have couple of questions. How to pass the data (which i got after the Alamofire request is finished), to one of children of TabBarController?

The first problem i have is that i can't override the func prepareForSegue inside login action(when the button is tapped), it says i can override only class members. But if i put the func outside of IBAction then i won't send the data that i need.

And the second problem is, when i put the overrided function outside of IBAction, and the code look like this:

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

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

when i run it, i got the error:

Could not cast value of type 'UITabBarController' to HomeViewController'

(HomeViewController is my destination view, where i should pass the data from Alamofire).

like image 950
anna.O Avatar asked Sep 14 '16 15:09

anna.O


People also ask

How do you pass data from one viewController to another using segue in Swift?

Pass Data using Seque. After outlet is connected to respective viewController, select Enter Last Name button and then CTRL + Drag to NextViewController(i.e, FirstViewController) for showing the viewController from LandingPageViewController. This lets you set what kind of segue you want to use.

How do I transfer data between Tab Bar Controllers?

Go to the storyboard. Select the single view controller by clicking on the title. In the drop-down menu, select Editor>Embed in>Tab Bar Controller. In this size class you can drag a drop and be pretty sure it will show correctly on a iPhone simulator.


1 Answers

You don't necessarily need to use prepareForSegue for this. Just reference which ViewController in the TabBarController viewControllers array that you want and cast it.

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

If the ViewControllers in your TabBar are embedded in Navigation Controllers, you can do this:

let navController = self.tabBarController.viewControllers![1] as! UINavigationController
let vc = navController.topViewController as! HomeViewController
vc.templateForCell = templates
like image 115
nighttalker Avatar answered Sep 24 '22 08:09

nighttalker