Pass data between ViewController and TabBarController

后端 未结 4 1465
旧巷少年郎
旧巷少年郎 2021-02-09 07:07

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

相关标签:
4条回答
  • 2021-02-09 07:33

    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 ^
    }
    
    0 讨论(0)
  • 2021-02-09 07:36

    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
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-09 07:40

    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
    
    0 讨论(0)
  • 2021-02-09 07:42

    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
    
    0 讨论(0)
提交回复
热议问题