Pass data between ViewController and TabBarController
Asked Answered
W

5

10

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

Winnow answered 14/9, 2016 at 15:34 Comment(1)
You can look at the viewControllers property of the UITabBarController to see if any of them are the kind of object you need to send to.Gailgaile
U
24

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
Ultracentrifuge answered 14/9, 2016 at 21:16 Comment(4)
But i get the error 'fatal error: unexpectedly found nil while unwrapping an Optional value'Winnow
Put this code in viewDidLoad and see what xcode prints: print(tabBarController?.viewControllers)Ultracentrifuge
this will work when you want to pass data from tab bar to one of the view controllers But how to pass data from view controller that embedded in navigation to the tab bar?Fanestil
@Ultracentrifuge i got nil. But the solution was, for anyone in the future, don't use self.tableBarController, simply use self.viewcontrollers.Donate
C
2

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
            }
        }
    }
}
Clave answered 1/9, 2017 at 15:24 Comment(0)
W
2

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

Weightless answered 24/1, 2023 at 15:39 Comment(0)
D
0

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 ^
}
Donate answered 18/8, 2020 at 5:53 Comment(0)
D
-1

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
Dryad answered 24/6, 2020 at 6:4 Comment(2)
That doesn't answer the questionDonate
here SearchViewController is the new view controller which has the variable selectedIndex and this code is passing data (indexPath.row) to the next view controller (search view controller) to its variable (selectedIndex).Dryad

© 2022 - 2024 — McMap. All rights reserved.