我有一个登录屏幕,该屏幕转到选项卡栏,然后切换到导航控制器。我需要将用户名传递给导航控制器的第一个视图控制器。就这样:
登录屏幕-> TabBarController-> NavigationController-> CustomerMainViewController
我到处都是堆栈溢出,并实现了以下代码,这似乎是共识:
//From my login View Controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let tabVC = segue.destination as! UITabBarController
let navVC = tabVC.viewControllers!.first as! UINavigationController
let destVC = navVC.viewControllers.first as! CustomerMainViewController
destVC.tempName = self.emailTextField.text!
}
@IBAction func loginButtonPresssed(_ sender: Any) {
self.performSegue(withIdentifier: "GoToTabBar", sender: self)
}
//From my View Controller (CustomerMainView Controller)
override func viewDidLoad() {
super.viewDidLoad()
username.text = self.tempName
}
我希望填充视图控制器上的用户名字段,但该字段为null。
答案 0 :(得分:0)
您的代码应该可以使用。当您定义了多个segue时,可以帮助检查segue标识符和/或安全地解开视图控制器:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToTabBar" {
if let tabVC = segue.destination as? UITabBarController,
let navVC = tabVC.viewControllers?.first as? UINavigationController,
let destVC = navVC.viewControllers.first as? CustomerMainViewController {
destVC.tempName = self.emailTextField.text!
}
}
}