计数器不会按下按钮?

时间:2016-11-10 13:19:16

标签: swift timer nstimer

每次调用endSetPressed操作时,我都试图将setCount设置为-1。

我已将setCount - = 1包含在updateTimer函数中,以便在按下endSetPressed时,它应该从setCount返回-1,然后启动restCount计时器。然而在实践中它似乎没有显示它在应用程序中执行-1它只是保持固定在起始setCount值。

另一个问题是我希望能够看到restRemainingCountdownLabel的值,因为用户正在设置restStepperValueChanged值,我认为这是通过' restRemainingCountdownLabel.text = String(restCount)'来完成的。然而,因为我正在使用' restCount = Int(sender.value)* 60'以分钟为单位生成一个值,它以秒为单位显示restRemainingCountdownLabel而不是分钟,同样对这一点有一些指导!

这是我的代码:

    @IBOutlet weak var restRemainingCountdownLabel: UILabel!
@IBOutlet weak var setsRemainingCountdownLabel: UILabel!
@IBOutlet weak var numberOfSetsLabel: UILabel!
@IBOutlet weak var numberOfRestLabel: UILabel!
@IBOutlet weak var adjustSetsStepper: UIStepper!
@IBOutlet weak var adjustRestStepper: UIStepper!

var restTimer: Timer?
var restCount = 0
var setCount = 0

@IBAction func endSetPressed(_ sender: Any) {
    restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}

@IBAction func setStepperValueChanged(_ sender: UIStepper) {
    numberOfSetsLabel.text = Int(sender.value).description
    setCount = Int(sender.value)
    setsRemainingCountdownLabel.text = String(setCount)
}

@IBAction func restStepperValueChanged(_ sender: UIStepper) {
    numberOfRestLabel.text = Int(sender.value).description
    restCount = Int(sender.value)*60
    restRemainingCountdownLabel.text = String(restCount)
}

@IBAction func resetSetsButton(_ sender: Any) {
   //setCount = Int
}

override func viewDidLoad() {
    super.viewDidLoad()

}

func updateTimer() {
    if (setCount > 0){
        setCount -= 1
    }
    if (restCount > 0){
        let minutes = String(restCount / 60)
        let seconds = String(restCount % 60)
        restRemainingCountdownLabel.text = minutes + ":" + seconds
        restCount -= 1
    }
}

1 个答案:

答案 0 :(得分:1)

查看您的updateTimer()方法,第一行:

if (setCount > 0)

如果setCount > 0则从setCount

中减去1

但是......在这里你宣布setCount的值为0

var setCount = 0

所以,你永远不会最终进入if部分:)

如何调试此

下次遇到这样的问题时,请尝试在代码中添加一些print()语句。

在你的情况下,你可以这样做:

@IBAction func endSetPressed(_ sender: Any) {
    print("endSetPressed")
    restTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RestController.updateTimer), userInfo: nil, repeats: true)
}

@IBAction func setStepperValueChanged(_ sender: UIStepper) {
    numberOfSetsLabel.text = Int(sender.value).description
    setCount = Int(sender.value)
    setsRemainingCountdownLabel.text = String(setCount)
}

@IBAction func restStepperValueChanged(_ sender: UIStepper) {
    numberOfRestLabel.text = Int(sender.value).description
    restCount = Int(sender.value)*60
    restRemainingCountdownLabel.text = String(restCount)
}

@IBAction func resetSetsButton(_ sender: Any) {
   //setCount = Int
}

override func viewDidLoad() {
    super.viewDidLoad()
}

func updateTimer() {
    print("updateTimer")
    if (setCount > 0){
        print("setCount > 0")
        setCount -= 1
        print("setCount is now \(setCount)")
    }
    if (restCount > 0){
        print("restCount > 0")
        let minutes = String(restCount / 60)
        let seconds = String(restCount % 60)
        restRemainingCountdownLabel.text = minutes + ":" + seconds
        restCount -= 1
        print("restCount is now \(restCount) - minutes: \(minutes) - seconds: \(seconds)")
    }
}

这应该会给你一些关于正在发生的事情以及"路径"你的代码如下。

希望对你有所帮助。