将Stepper值传递给另一个视图控制器

时间:2020-03-13 23:42:22

标签: swift view controller stepper

很容易解释的代码,我的问题是第一张打印纸显示正确的值,但第二张打印纸始终显示0。我试图将步进器的值传递给我的下一个视图控制器(calcVC)。

编辑:忘记提及我的问题是,当我单击一个按钮时触发了CalcVC的正常运行,这就是我释放该值时发生的。另外,这段代码在TableViewCellVC中,而我的选择是从TableViewVC到CalcVC,这就是为什么我在CalcVC上遇到问题的原因。

最终编辑:通过创建全局变量对其进行了修正

    var stepperValue = 1


@IBAction func stepperValueChanged(_ sender: Any) {

    stepperValue = Int(stepper.value)

    print(stepperValue)

    stepperLabel.text = String(stepperValue)

    let CalcVC = CalculatorVC()

    stepperValue = CalcVC.calcVCStepperValue

    print(stepperValue)
}

1 个答案:

答案 0 :(得分:2)

您需要将#Global constants TIMESTORUN=20 #This program analyzes 20 inputted numbers and prints them like so: lowest, highest, total, average. def main(): userInput() def userInput(): counter=1 listOfNumbers=[0]*20 while counter<=TIMESTORUN: listOfNumbers=int(input('Enter a number ')) counter+=1 minNumber=min(listOfNumbers) print('The smallest number is', minNumber) maxNumber=max(listOfNumbers) print('The largest number is', maxNumber) total=0 for number in listOfNumbers: total+=number print('The sum is', total) average=sum/len(listOfNumbers) print('The average is', average) main() 的值分配给stepperValue。目前,您只是在创建CalcVC.calcVCStepperValue,然后打印出CalcVC的默认值。

calcVCStepperValue

如果使用segues进行此操作,则无法创建视图控制器的新实例。相反,您需要在要解散的视图控制器上实现let CalcVC = CalculatorVC() CalcVC.calcVCStepperValue = stepperValue stepperValue = CalcVC.calcVCStepperValue print(stepperValue) 函数,并使用segue参数访问prepareForSegue

Passing data with unwind segue

对此已有答案。
相关问题