我还添加了设计的屏幕截图。
我有一个ViewController A和一个带有2个View Controller B,C的标签栏。
我想通过标签栏从VcA-> VcB传递data
变量的数据。
我已将序列从submitbutton
拖到TabBar
。
我试图通过segue传递数据
所以,请帮助我。
//In View controller A
class ViewControllerA : UIViewController{
var data : String = ""
@IBAction func submitButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "homePage", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = self.tabBarController!.viewControllers![0] as! ViewControllerB
vc.containsData = data
}
}
//In Tabbar View controller B
class ViewControllerB: UIViewController {
var containsData : String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(containsData)
}
}
在标签栏的viewcontrollerB中,我刚刚在viewDidLoad()中打印了var“数据”
答案 0 :(得分:1)
查看代码,我发现了问题所在。
首先,您需要使用supplier_price
方法来传递数据,因为您正在使用segue展示These throw same error:
$params[] = "supplier_price";
$params[] = "`supplier_price`";
。因此,在您的prepare(for segue
中,添加此方法并为viewControllerb
设置数据,如下所示,
ViewController
第二,从ViewControllerb
中删除方法override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let tabController = segue.destination as? UITabBarController {
let bController = tabController.children.first(where: { $0 is ViewControllerb })
(bController as? ViewControllerb)?.containsData = self.stringTF.text!
}
super.prepare(for: segue, sender: sender)
}
,因为您正在使用segue呈现ViewControllerb,所以这不是必需的。也可以通过单击十字,在情节提要中删除此方法插座,如下图所示
答案 1 :(得分:1)
我得到了答案
@IBAction func submitButonTapped(_ sender: Any) {
performSegue(withIdentifier: "homePage", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationViewController = barViewControllers.viewControllers![0] as! ViewControllerB
destinationViewController.containsData = Data
}
答案 2 :(得分:0)
如果要通过NavigationController传递数据viewController-> Tabcontroller。请尝试在按钮/ tableviewCell / collectionViewCell单击/按下
let navController = self.Your_TABBAR_CONTROLLER.viewControllers![Tab_Index] as! UINavigationController
let vc = navController.topViewController as! Your_View_Controller
vc.name = Data
let navController = self.tabBarController.viewControllers![1] as! UINavigationController
let vc = navController.topViewController as! HomeViewController
vc.name= self.name