1

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)
}
AvsBest
  • 435
  • 1
  • 4
  • 9

1 Answers1

2

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

Calvedos
  • 349
  • 2
  • 6
  • According to breakpoints, this works however I neglected to mention that my issue happens when I click a button which triggers and unwind segue to CalcVC which is when I loose the value. I updated my original post to reflect that – AvsBest Mar 14 '20 at 01:58
  • Updated my answer :) – Calvedos Mar 14 '20 at 02:13
  • Fixed by creating a global variable. Thanks – AvsBest Mar 14 '20 at 04:16