How do I pass data from a tab bar controller to one of its tabs?

后端 未结 6 767
滥情空心
滥情空心 2021-02-02 09:21

I have a UITabBarController set up in storyboard. I want to pass a dictionary of data from the tab bar controller for use in the appropriate child tab, which is a standard UIVie

6条回答
  •  迷失自我
    2021-02-02 10:26

    Using Swift

    If you have UINavigationController as one of the tabs in UITabBarController, you want to get the instance of the child view controller instead of creating a new instance and push it to the navigation view controller before changing the tab view index. You can do this...

    This sample code assumes the Navigation Controller is at the first tab (index 0) and "childViewController" is embedded in the Navigation Controller.

    let viewControllers = self.tabBarController?.viewControllers
    
    let navController = viewControllers![0] as! UINavigationController
    
    let profileViewController = self.storyboard?.instantiateViewControllerWithIdentifier("childViewController") as? ProfileViewController
    
    profileViewController!.parameter1 = "Value1"
    
    profileViewController!.parameter2 = "Value2"
    
    navController.pushViewController(profileViewController!, animated: true)
    
    self.tabBarController?.selectedIndex = 0
    

提交回复
热议问题