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
Andrew Bernard
23,262 PointsSwift - 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
Michael Hulet
47,913 PointsPersonally, 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.
Andrew Bernard
23,262 PointsPerfect!!!! Works like a charm!
Michael Hulet
47,913 PointsAwesome! Glad I could help!
Andrew Bernard
23,262 PointsAndrew Bernard
23,262 PointsI like your idea but I am getting two errors. Suggestions?
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsI 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: