p.yarandi p.yarandi - 4 months ago 21
Swift Question

Passing data between tabs of UITabBar

Storyboard screenshot with description

I've used this line of code in the

UIViewController
class of 2nd tab to have access to another
tabBarViewController
. But it crashes.

let friendView = self.tabBarController?.viewControllers![2] as! AccountTableViewController


Note:
AccountTableViewController
is the name of
UITableViewController
class of 3rd tab.
Thanks for your help.

Answer

Change your code like this

let navController = self.tabBarController?.viewControllers![2] as! UINavigationController
for vc in (self.navigationController?.viewControllers)! {
    if (vc.isKindOfClass(AccountTableViewController.classForCoder())) {
        let friendView = vc as! AccountTableViewController
    }
}
Comments