我试图从视图控制器的swift文件中获取这些变量:

 //global
let choice1Box1 = drivingGear[chooseDrivingGear.selectedRow(inComponent: 0)]
let choice2Box1 = drivingGear[chooseDrivenGear1.selectedRow(inComponent: 0)]

这是对原始类中的变量所做的操作:
@IBAction func showResultBox1(_ sender: Any) {

        let choice1Box1 = self.drivingGear[self.chooseDrivingGear.selectedRow(inComponent: 0)]
        let choice2Box1 = self.drivingGear[self.chooseDrivenGear1.selectedRow(inComponent: 0)]
        if let intVal1 = Double(choice1Box1), let intVal2 = Double(choice2Box1) {
            result = intVal2 / intVal1

            let newLabel = String(result)
            resultBox1.setTitle(newLabel, for: .normal)
        }
    }

最佳答案

就这样做:
从pickerView中选择值后。像这样传递。
例如,你需要给下一个VC传递一个字符串:
在SecondVC中:在顶部声明一个值,如下所示:

var strFromPreviousVC:String = String()

在第一个VC中:发送如下值:
let objSecondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondVC
objSecondVC.strFromPreviousVC = "your selected String here"
    self.navigationController?.pushViewController(objSecondVC, animated: true)

是的,它完成了,它将被传递给SecondVc,您可以在SecondVc中使用类似print(strFromPreviousVC)的值
希望有帮助。

10-06 10:10