Search code examples
uiviewcontrollerswift2uitabbarcontrollerxcode7uistoryboardsegue

passing data from UITabBarView Class to sub class of UIViewController using swift


I am trying to pass data from my TabViewClass to sub Class of its Tab ViewController Here is my TabBar Class

class TabBarController : UITabBarController, UITabBarControllerDelegate{

var unitId : Int = 0;

override func viewDidLoad() {
    super.viewDidLoad()
    print("TabBarVC Called")
    unitId = 10;
    print("Unit Id = \(unitId)")

    self.delegate = self;

} }

after running this viewDidLoad() my sub class of first Tab Bar automatically called its viewDidLoad() .. how can I share my major Tab Bar Controller class data into its sub Tab class (View Controllers) .. using swift


Solution

  • Don't know it is better way or not, I have passed the data this way:

    class TabBarController : UITabBarController, UITabBarControllerDelegate{
    var unitId : Int = 0;
    
    override func viewDidLoad() {
    super.viewDidLoad()
    print("TabBarVC Called")
    unitId = 10;
    print("Unit Id = \(unitId)")
    
    self.delegate = self;
    
    //Pass data to all tab controller
    let MovementRepVC: MovementRep = self.viewControllers![0] as! MovementRep
    MovementRepVC.id = self.unitId
    
    let secondVC: UIViewController = self.viewControllers![1] as! UIViewController
    secondVC.data = self.myData
    
    } }
    

    Hope It will help you.