-1

Forgive the mess; I may have extra code from trying to do this by using examples I found on here, but none seem to work.

I have a stepper

@IBOutlet weak var turnsRemaining: UILabel! @IBOutlet weak var turnStepper: UIStepper!\

I have these functions:

@IBAction func turnStepperChanged(_ sender: UIStepper) { turnsRemaining.text = Int(sender.value).description }

What I want is to click a button called "leftRight" then triggers a function (which I have working) but I want to add to that function that the value of the stepper is decreased by one.

I tried adding the following line in the code as part of the function (a different function than the one listed above):

turnStepper.value = turnsRemaining.text -1
JMC
  • 1
  • 1
  • what programming language is this and what is the exact error message? Please [edit] your question to include those details. – rene Jun 22 '19 at 10:10
  • Swift Cannot assign value of type 'String?' to type 'Double' – JMC Jun 22 '19 at 11:45
  • Possible duplicate of [Cannot assign value of type 'String?' to type 'Double' error](https://stackoverflow.com/questions/43344796/cannot-assign-value-of-type-string-to-type-double-error) – rene Jun 22 '19 at 11:54
  • That thread does not have a solution that seems to work. – JMC Jun 22 '19 at 12:35

2 Answers2

0

This is the problem:

@IBAction func turnStepperChanged(_ sender: UIStepper) {
    turnsRemaining.text = Int(sender.value).description
}

It isn't just that that isn't the way to turn a Double into a String. It's that you are mediating directly from the stepper value to the label text.

Model view controller. The stepper is view. The label is view. Neither of them is the source of truth. You need a property that plays that role. There must always be an instance property maintaining this value. When it changes, the stepper and label change to match.

Once you have that, any code can just reach out and change that property to change the stepper and label together, keeping them always in sync.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So how does one fit that? I also have this: @IBAction func stepperValueChanged(_ sender: UIStepper) { turnsBeforeStop = turnsRemaining.text! but I am not sure if that func is being used at this point. – JMC Jun 22 '19 at 15:07
-1

Try casting your 'turnsRemaning.text' to a double:

turnStepper.value = Double(turnsRemaining.text)
Jason
  • 284
  • 2
  • 10