从UIDatePicker获取时间

时间:2016-10-31 19:27:40

标签: ios swift

我想知道如何在倒数计时器模式下从UIDatePicker中获取时间,然后在以小时:分钟:秒格式倒计时显示该时间。我最大的问题实际上是从日期选择器中获取时间。因为我不确定如何开始。

override func viewDidLoad() {
super.viewDidLoad()

stopWatchPicker.countDownDuration = 60.0;

    if timerCount == 0 { 
        timerRunning = false
    }
}

var timerCount = 60 //set to 60 for testing purposes
var timer = Timer() 
var timerRunning = false 


@IBOutlet weak var stopWatchPicker: UIDatePicker!  
@IBOutlet weak var countDownLabel: UILabel!

@IBAction func startButton(_ sender: AnyObject) {
    if timerRunning == false {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
        timerRunning = true
    }
}
@IBAction func stopButton(_ sender: AnyObject) {
    timer.invalidate()
    timerRunning = false
}

func updateCounter() {
    timerCount -= 1
    countDownLabel.text = "\(timerCount)"

    if timerCount == 0 {
        timer.invalidate()
        timerRunning = false
    }
}

1 个答案:

答案 0 :(得分:0)

您需要从stopWatchPicker获取countDownDuration并将其分配给timerCount变量。

@IBAction func startButton(_ sender: AnyObject) {
  if timerRunning == false {
    timerCount = stopWatchPicker.countDownDuration
    timerRunning = true
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
  }
}

干杯!