[英]why my stepper does't give the correct value when I tap the increment button?
I am using kw stepper pod, since it is customizable.我正在使用 kw stepper pod,因为它是可定制的。 I can separate the increment button, decrement button and using my own label.
我可以将递增按钮、递减按钮和使用我自己的标签分开。 but the behaviour should be the same as UIStepper
但行为应该与 UIStepper 相同
that is what it looks like, it consists of 1 increment button, 1 decrement button and counter label.这就是它的样子,它由 1 个递增按钮、1 个递减按钮和计数器标签组成。
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.我使用 @IBAction touch up 内部事件连接增量和减量按钮。
so I expect when I tap the increment button, it will increase from 0,0 -> 1.0 -> 2.0 -> 3.0 and so on.所以我希望当我点击增量按钮时,它会从 0,0 -> 1.0 -> 2.0 -> 3.0 等增加。
but in my case, when tap the increment button it will give 0,0 -> 0,0 -> 1,0 -> 2,0但就我而言,当点击增量按钮时,它会给出 0,0 -> 0,0 -> 1,0 -> 2,0
the 0,0 will appear twice. 0,0 会出现两次。 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这是我的谷歌驱动器上的项目: https : //drive.google.com/file/d/1IgeVW1OemRttoAOqJ6Ba8LpyZC_rc3-o/view?usp=sharing
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.可能在
stepper.value
更改之前调用incrementButtonDidTapped
和decrementButtonDidTapped
方法,因为KWStepper
还侦听来自这两个按钮的touchUpInside
事件以更改其值。
KWStepper
exposes two properties decrementCallback
and incrementCallback
which you can use to get notified when the value gets decremented/incremented. KWStepper
公开了两个属性decrementCallback
和incrementCallback
,您可以使用它们在值递减/递增时获得通知。 You can use these instead of an IBAction
on the two buttons.您可以在两个按钮上使用它们而不是
IBAction
。
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.或者,您可以确认
KWStepperDelegate
并实现KWStepperDidIncrement
和KWStepperDidDecrement
委托方法以获取通知。
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)"
}
}
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)"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.