简体   繁体   中英

How to get data from UIDatePicker countDownTimer mode

I have UIDatePicker in countDownTimer mode on AlertController. And I want to save values, that user will choose when he press Set button, to variable. How and were can I do it? My code now:

func showAlert() {
    let vc = UIViewController()
    vc.preferredContentSize = CGSize(width: 250,height: 300)
    let pickerView = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
    pickerView.datePickerMode = .countDownTimer
    vc.view.addSubview(pickerView)
    let alert = UIAlertController(title: "Sleep Timer", message: "Please, choose hours and minutes", preferredStyle: UIAlertController.Style.alert)
    alert.setValue(vc, forKey: "contentViewController")
    alert.addAction(UIAlertAction(title: "Set", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true)
}

Generally, you can access the picker's countDownDuration property to get the seconds value.

It ranges between 0 to 86,340 seconds.

let seconds = pickerView.countDownDuration

For your required case, you have a lot of options. 2 of them are:

Easier

    func showAlert() {
        let vc = UIViewController()
        vc.preferredContentSize = CGSize(width: 250,height: 300)
        let pickerView = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
        pickerView.datePickerMode = .countDownTimer
        //Your required line
        pickerView.addTarget(self, action: #selector(onDurationChanged), for: .valueChanged)
        vc.view.addSubview(pickerView)
        let alert = UIAlertController(title: "Sleep Timer", message: "Please, choose hours and minutes", preferredStyle: UIAlertController.Style.alert)
        alert.setValue(vc, forKey: "contentViewController")
        alert.addAction(UIAlertAction(title: "Set", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true)
    }
    
    @objc private func onDurationChanged(_ picker: UIDatePicker) {
        let seconds = picker.countDownDuration
        //Do something with duration
    }

Cleaner way

You could just create a small VC for the picker selection and then you can separate the concerns better:

class PickerViewController: UIViewController {
    var duration: TimeInterval = 0
    
    let pickerView: UIDatePicker = {
        let picker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
        picker.datePickerMode = .countDownTimer
        picker.addTarget(self, action: #selector(onDurationChanged), for: .valueChanged)
        return picker
    }()
    
    @objc private func onDurationChanged(_ picker: UIDatePicker) {
        duration = picker.countDownDuration
    }
}

class ViewController: UIViewController {
    func showAlert() {
        let vc = PickerViewController()
        vc.preferredContentSize = CGSize(width: 250,height: 300)
        let alert = UIAlertController(title: "Sleep Timer", message: "Please, choose hours and minutes", preferredStyle: UIAlertController.Style.alert)
        alert.setValue(vc, forKey: "contentViewController")
        alert.addAction(UIAlertAction(title: "Set", style: .default, handler: { action in
            let seconds = vc.duration
            //Do something.
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM