如何在Date Picker swift中有小时:分钟:秒

时间:2017-12-16 09:58:47

标签: ios swift datepicker

我编写了一个具有计时器的应用程序,用户可以设置计时器的时间(在选择器视图上)并且它将倒计时。所以在日期选择器中我选择倒计时模式。但我不知道如何工作有没有任何身体可以帮助我?我很快就这么新。事实上,我希望用户设置时间选择器,然后当点击开始时开始倒计时但是日期选择器只有小时和分钟而且没有秒,我该怎么办?我可以将选择器视图的时间设置为标签,但选择器视图没有第二个

let timeLeft = datePicker.date
    let formatter = DateFormatter()
    formatter.dateFormat = "HH:mm:ss"
    let formatteddate = formatter.string(from: timeLeft)
    countinLabel.text = formatteddate

2 个答案:

答案 0 :(得分:9)

您应该尝试UIPickerView 小时:分钟:秒格式。
因为: UIDatePicker支持的唯一支持时间格式为小时:分钟

class ViewController: UIViewController {

    @IBOutlet weak var toolbar: UIToolbar!

    @IBOutlet weak var picker: UIPickerView!

    var hour:Int = 0
    var minutes:Int = 0
    var seconds:Int = 0

}

DataSource和Delegate如下:

extension ViewController:UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return 25
        case 1,2:
            return 60

        default:
            return 0
        }
    }

    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return pickerView.frame.size.width/3
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return "\(row) Hour"
        case 1:
            return "\(row) Minute"
        case 2:
            return "\(row) Second"
        default:
            return ""
        }
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            hour = row
        case 1:
            minutes = row
        case 2:
            seconds = row
        default:
            break;
        }
    }
}

答案 1 :(得分:2)

上一个答案中的正确代码,可以在swift 5.1和xcode 11.1上运行:

class TimerSelectViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        pickerView.delegate = self
    }

    @IBOutlet var pickerView: UIPickerView!

    var hour: Int = 0
    var minutes: Int = 0
    var seconds: Int = 0

}

     extension TimerSelectViewController: UIPickerViewDelegate, UIPickerViewDataSource {

         func numberOfComponents(in pickerView: UIPickerView) -> Int {
             return 3
         }

         func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
             switch component {
             case 0:
                 return 25
             case 1, 2:
                 return 60
             default:
                 return 0
             }
         }

         func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
             return pickerView.frame.size.width/3
         }

         func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
             switch component {
             case 0:
                 return "\(row) Hour"
             case 1:
                 return "\(row) Minute"
             case 2:
                 return "\(row) Second"
             default:
                 return ""
             }
         }
         func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
             switch component {
             case 0:
                 hour = row
             case 1:
                 minutes = row
             case 2:
                 seconds = row
             default:
                 break;
             }
         }
     }