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

Shades
  • 5,568
  • 7
  • 30
  • 48
anna.O
  • 101
  • 1
  • 1
  • 7
  • 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. – Phillip Mills Sep 14 '16 at 16:43

5 Answers5

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
nighttalker
  • 896
  • 6
  • 13
  • 1
    But i get the error 'fatal error: unexpectedly found nil while unwrapping an Optional value' – anna.O Sep 20 '16 at 10:40
  • Put this code in viewDidLoad and see what xcode prints: `print(tabBarController?.viewControllers)` – nighttalker Sep 20 '16 at 21:23
  • 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? – Saeed Rahmatolahi Feb 03 '19 at 17:37
  • @nighttalker i got nil. But the solution was, for anyone in the future, don't use self.tableBarController, simply use self.viewcontrollers. – Yash Jain Aug 15 '20 at 04:01
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
            }
        }
    }
}
Dave Stevens
  • 106
  • 2
  • 6
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

Priyank Patel
  • 791
  • 8
  • 6
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 ^
}
Yash Jain
  • 442
  • 7
  • 19
-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
Muhammad Aakif
  • 193
  • 2
  • 5
  • That doesn't answer the question – Yash Jain Aug 15 '20 at 03:45
  • 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). – Muhammad Aakif Aug 17 '20 at 05:34