anna.O
anna.O

Reputation: 101

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).

Upvotes: 10

Views: 15765

Answers (5)

Priyank Patel
Priyank Patel

Reputation: 870

For Swift5

If you want to pass data on Smart(Tabbar index -3)

 let navController = self.tabBarController?.viewControllers?[3] as! UINavigationController
 let vc = navController.topViewController as! SmartDashboard
     vc.isWantToSmartTab = true //Pass any data what you want
     vc.number = 2
     self.tabBarController?.selectedIndex = 3

enter image description here

Upvotes: 2

Muhammad Aakif
Muhammad Aakif

Reputation: 212

change tabbar selected Index & send data to that ViewController

tabBarController!.selectedIndex = 2
let navVC = tabBarController!.viewControllers![2] as! UINavigationController

let SV = navVC.topViewController as! SearchViewController
SV.selectedIndex = indexPath.row

Upvotes: -1

Yash Jain
Yash Jain

Reputation: 461

You could do something like this from the TabController class (parent):

var maleVC: MaleOptionsViewController! //First child 
var femaleVC: FemaleOptionsViewController! //Second child

override func viewDidLoad() {
    super.viewDidLoad()
    maleVC = self.viewControllers![0] as? MaleOptionsViewController //Reference to first child
    femaleVC = self.viewControllers![1] as? FemaleOptionsViewController //Reference to second child
}

And pass it data like this:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        maleVC.cartTableView?.reloadData() //Where cartTableView is an IBOutlet or even a variable for that matter
        femaleVC.cartTableView?.reloadData() //Same thing as the comment ^
}

Upvotes: 0

Dave Stevens
Dave Stevens

Reputation: 106

For Xcode 8, Swift 3.x you can use something like the following. This assumes you have your view controller embedded in a navigation controller. In my situation, I am trying to set a variable called startWizard to true when navigating from the new user setup view.

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
            }
        }
    }
}

Upvotes: 2

nighttalker
nighttalker

Reputation: 946

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

Upvotes: 24

Related Questions