Michael R
Michael R

Reputation: 93

Pass Data From ViewController to TabBarController

I need to pass a username from the View Controller login screen to by Tab Bar Controller textfield.

I have tried the traditional method of doing this and am receiving a

"Could not cast value of type UITabBarController message.

If I don't try to pass the data, the tab bar controller loads fine.

//In my View Controller, I have tried this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "GoToTabBar" {
        let custMainVC = segue.destination as! CustomerMainViewController
        custMainVC.tempName = self.emailTextField.text!

    }
}

//In my tab bar controller whose first View Controller has a name of CustomerMainViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    username.text = self.tempName
}

Outcome is a Thread 1 and the debugger states: Reading from private effective user settings. Could not cast value of type 'UITabBarController'

I have cleared all my outlets and all the simple things that you may recommend, and as I stated if I remove the code to pass the data it is loading so this is not an outlet issue. Thanks for your help!!

Upvotes: 0

Views: 1170

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

The segue destination is the tabBar access it then you can get the first vc and pass the data

let custMainVC = segue.destination as! UITabBarController
let res = custMainVC.viewControllers!.first as! CustomerMainViewController
res.tempName = self.emailTextField.text!

Upvotes: 1

Related Questions