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