Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift - How to get Two UISteppers of different step values to change one label.

Here are my two steppers and a little of my tinkering:

    @IBAction func plusOneStepper(sender: UIStepper) {
        plusOneLabel.text = Int(sender.value).description

        totalLabel.text = "\(plusOneLabel) + \(plusTwoLabel)"
        println(totalAwayScore.text)
    }

    @IBAction func plusTwoStepper(sender: UIStepper)  {
        plusTwoLabel.text = Int(sender.value).description
    }

2 Answers

Personally, this is the way I'd do it:

@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var plusOneStepper: UIStepper!
@IBOutlet weak var plusTwoStepper: UIStepper!
override func viewDidLoad() -> Void{
    plusOneStepper.maximumValue = 1
    plusOneStepper.minimumValue = -1
    plusTwoStepper.maximumValue = 2
    plusTwoStepper.minimumValue = -2
    plusTwoStepper.stepValue = 2
    //Make sure that countLabel has a number value at runtime. You could alternatively save the value with NSUserDefaults each time the view disappears, and update it again here
    countLabel.text = "0"
}
func resetStepper(stepper: UIStepper) -> Void{
    stepper.value = 0
}
func incrementCount(stepper: UIStepper) -> Void{
    countLabel.text = "\(totalCount() + Int(stepper.value))"
    resetStepper(stepper)
}
func totalCount() -> Int{
    return countLabel.text!.toInt()!
}
@IBAction func plusOneStepper(sender: UIStepper) -> Void{
    incrementCount(sender)
}
@IBAction func plusTwoStepper(sender: UIStepper) -> Void{
    incrementCount(sender)

That way, the label holds the central value, and the steppers only increment it by their assigned value, up or down.

I like your idea but I am getting two errors. Suggestions?

//'Double' is not convertible to an Int
    func incrementCount(stepper: UIStepper) -> Void{
            countLabel.text = "\(Int(totalCount() + stepper.value))"
            resetStepper(stepper)
        }
//'String?' does not have a member named 'toint'
        func totalCount() -> Int{
            return countLabel.text.toInt()
         }

I kinda figured something might end up wrong, because I didn't even put it into a playground to test it. I did now, and I edited my answer to fix those 2 problems. The 2 notable changes:

func incrementCount(stepper: UIStepper) -> Void{
    //Instead of typecasting the whole thing to an Int, we just cast the thing that isn't already an Int (stepper.value, which is naturally a Double)
    countLabel.text = "\(totalCount() + Int(stepper.value))"
    resetStepper(stepper)
}

func totalCount() -> Int{
    //text and toInt() are both optionals, so they need to be unwrapped. Since it's guaranteed they're always going to be something we want, it's safe to explicitly unwrap them
    return countLabel.text!.toInt()!
}

Perfect!!!! Works like a charm!

Awesome! Glad I could help!