将数据从 ViewController 传递到 TabBarController

Pass Data From ViewController to TabBarController

我需要将用户名从 View Controller 登录屏幕传递到 Tab Bar Controller 文本字段。

我已经尝试过这样做的传统方法,并且收到了

"Could not cast value of type UITabBarController message.

如果我不尝试传递数据,标签栏控制器加载正常。

//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
}

结果是线程 1,调试器状态:从私有有效用户设置中读取。 无法转换 'UITabBarController'

类型的值

我已经清除了我所有的插座和您可能推荐的所有简单的东西,正如我所说,如果我删除代码以传递它正在加载的数据,那么这不是插座问题。感谢您的帮助!!

segue 目标是 tabBar 访问它然后你可以得到第一个 vc 并传递数据

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