Reputation: 391
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)
}
Upvotes: 0
Views: 913
Reputation: 325
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:
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
}
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)
}
}
Upvotes: 1