Reputation: 625
Pretty self explanatory code, my issue is the first print shows the correct value but the second print always show 0. I'm trying to pass the value of the stepper to my next view controller (calcVC).
Edit: Forgot to mention that my issue happens when I click a button which triggers an unwind segue to CalcVC which is when I loose the value. Also, this code is in TableViewCellVC and my unwind segue is from TableViewVC to CalcVC which is why I'm having issue on CalcVC.
Final Edit: Fixed it by creating a global variable
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)
}
Upvotes: 1
Views: 293
Reputation: 349
You need to assign the value of stepperValue
to CalcVC.calcVCStepperValue
. At the moment you are just creating CalcVC
then printing out the default value of calcVCStepperValue
.
let CalcVC = CalculatorVC()
CalcVC.calcVCStepperValue = stepperValue
stepperValue = CalcVC.calcVCStepperValue
print(stepperValue)
If you’re doing this with segues you can’t create a new instance of the view controller. Instead you need to implement the prepareForSegue
function on the view controller from which you are unwinding and use the segue parameter to access the destinationViewController
.
There is an existing answer for this at Passing data with unwind segue
Upvotes: 2