Reputation: 13833
I am using kw stepper pod, since it is customizable. I can separate the increment button, decrement button and using my own label. but the behaviour should be the same as UIStepper
that is what it looks like, it consists of 1 increment button, 1 decrement button and counter label.
here is the code on my view controller:
import UIKit
import KWStepper
class ViewController: UIViewController {
@IBOutlet weak var counterLabel: UILabel!
@IBOutlet weak var decrementButton: UIButton!
@IBOutlet weak var incrementButton: UIButton!
var stepper: KWStepper!
override func viewDidLoad() {
super.viewDidLoad()
stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)
stepper.autoRepeat = false
stepper.autoRepeatInterval = 1
stepper.wraps = false
stepper.minimumValue = 0
stepper.maximumValue = 100
stepper.incrementStepValue = 1
stepper.decrementStepValue = 1
stepper.value = 0.0
counterLabel.text = "\(stepper.value)"
}
@IBAction func incrementButtonDidTapped(_ sender: Any) {
counterLabel.text = "\(stepper.value)"
}
@IBAction func decrementButtonDidTapped(_ sender: Any) {
counterLabel.text = "\(stepper.value)"
}
}
I connect the increment and decrement button using @IBAction touch up inside event.
so I expect when I tap the increment button, it will increase from 0,0 -> 1.0 -> 2.0 -> 3.0 and so on.
but in my case, when tap the increment button it will give 0,0 -> 0,0 -> 1,0 -> 2,0
the 0,0 will appear twice. why it appears twice ? how to solve this issue
I know that I can see the stepper value from value change event like this
stepper
.valueChanged { stepper in
// ...
}
but I need to separate the event from increment and decrement button
here is the project on my google drive: https://drive.google.com/file/d/1IgeVW1OemRttoAOqJ6Ba8LpyZC_rc3-o/view?usp=sharing
Upvotes: 2
Views: 1108
Reputation: 604
The incrementButtonDidTapped
and decrementButtonDidTapped
methods are possibly being called before stepper.value
changes, since KWStepper
also listens for touchUpInside
events from both of these buttons to change its value.
KWStepper
exposes two properties decrementCallback
and incrementCallback
which you can use to get notified when the value gets decremented/incremented. You can use these instead of an IBAction
on the two buttons.
stepper.decrementCallback = { (stepper) in
self.counterLabel.text = "\(stepper.value)"
}
stepper.incrementCallback = { (stepper) in
self.counterLabel.text = "\(stepper.value)"
}
Alternatively, you can confirm to KWStepperDelegate
and implement KWStepperDidIncrement
and KWStepperDidDecrement
delegate methods to get notified.
import UIKit
import KWStepper
class ViewController: UIViewController, KWStepperDelegate {
@IBOutlet weak var counterLabel: UILabel!
@IBOutlet weak var decrementButton: UIButton!
@IBOutlet weak var incrementButton: UIButton!
var stepper: KWStepper!
override func viewDidLoad() {
super.viewDidLoad()
stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)
stepper.autoRepeat = false
stepper.autoRepeatInterval = 1
stepper.wraps = false
stepper.minimumValue = 0
stepper.maximumValue = 100
stepper.incrementStepValue = 1
stepper.decrementStepValue = 1
stepper.value = 0.0
// Set the delegate
stepper.delegate = self
counterLabel.text = "\(stepper.value)"
}
@objc func KWStepperDidIncrement() {
counterLabel.text = "\(stepper.value)"
}
@objc func KWStepperDidDecrement() {
counterLabel.text = "\(stepper.value)"
}
}
Upvotes: 1
Reputation: 90
You can replace your click events with following code
@IBAction func incrementButtonDidTapped(_ sender: Any) {
stepper.valueChanged { (steper) in
self.counterLabel.text = "\(steper.value)"
}
}
@IBAction func decrementButtonDidTapped(_ sender: Any) {
stepper.valueChanged { (steper) in
self.counterLabel.text = "\(steper.value)"
}
}
Upvotes: 0