我正在尝试在具有多个定时器的tableview中使用NSTimer

时间:2016-03-14 15:14:40

标签: ios swift nstimer

然而,当我启动计时器时,它似乎反复运行,但它不会减少计数器。我试图通过计数器将count变量传递到选择器,但似乎计数器每次都重置,而不是继续减少。我是编程的新手,所以虽然我希望它是愚蠢的,但我可能在组织上有所错...我的代码是:

func timerDidEnd(timer: NSTimer) {

var timeCount = timer.userInfo as! Double

timeCount -= timeInterval

if timeCount <= 0 {  //test for target time reached.
    print("Timer = 0")
    timer.invalidate()
} else { //update the time on the clock if not reached
    print("Timer Count: \(timeCount)")
}


extension ViewController: TimerTableViewCellDelegate {

func startTimer(indexPath: NSIndexPath!) {
    print("timer \(indexPath.row) button started")

    var currentTimer = baseArray[indexPath.row]

    currentTimer.timeCount = Double((currentTimer.duration[0] * 60) + (currentTimer.duration[1]) + currentTimer.duration[2])

    currentTimer.timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: "timerDidEnd:", userInfo: currentTimer.timeCount, repeats: true)

}

func stopTimer(indexPath: NSIndexPath!) {

    let currentTimer = baseArray[indexPath.row]

    print("Stop Timer")
    currentTimer.timer.invalidate()

}

1 个答案:

答案 0 :(得分:0)

我想出来了 - 我更新了我的计时器结构以包含正确的时间计数,这在将indexPath作为userInfo传递时起作用