swiftuinavigationcontrolleruitabcontroller

Swift passing data between Nav and Tab Controller


A viewcontrollerB is the root view of nav controller and first view of tab view controller at same time. How to pass a data from initial viewA through nav and tab controller to ViewB ? Thanks

Storyboard


Solution

  • Give your tab bar controller a custom class, for example MyTabBarController, and declare a variable there that will receive the text from your textfield in log in view controller (via prepareForSegue). then you will be able to access that variable from your home view controller.

    In your log in view controller:

        @IBAction func ButtonPressed(sender: AnyObject) {
    
        performSegueWithIdentifier("toTabBarController", sender: nil)
    
        }
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            if let tabBarController = segue.destinationViewController as? MyTabBarController{
    
            tabBarController.someVariable = someTextField.text!
    
            }
    
        }
    

    in your tab bar controller:

    var someVariable = String()
    

    in you home tableview controller:

        override func viewWillAppear(animated: Bool) {
    
            let tabBarController = self.tabBarController as! MyTabBarController
            someVariable = tabBarController.someVariable
    
        print("the text is",someVariable)
    
    
    }