Как получить данные из режима UIDatePicker countDownTimer - PullRequest
0 голосов
/ 04 августа 2020

У меня UIDatePicker в режиме countDownTimer на AlertController. И я хочу сохранить значения, которые пользователь выберет, когда он нажмет кнопку «Установить», в переменную. Как и где я могу это сделать? Мой код сейчас:

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)
}

1 Ответ

1 голос
/ 04 августа 2020

Как правило, вы можете получить доступ к свойству средства выбора countDownDuration , чтобы получить значение секунд.

Оно находится в диапазоне от 0 до 86,340 секунд.

let seconds = pickerView.countDownDuration

Для вашего необходимого случая у вас есть много вариантов. Два из них:

Более простой

    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
    }

Более чистый способ

Вы можете просто создать небольшой V C для выбора средства выбора, а затем вы можете разделить проблемы лучше:

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)
    }
}
...