ios - 如何从 UIDatePicker countDownTimer 模式获取数据
问题描述
我在 AlertController 上有处于 countDownTimer 模式的 UIDatePicker。我想保存值,用户在按下设置按钮时会选择变量。我该怎么做?我现在的代码:
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)
}
解决方案
通常,您可以访问选择器的countDownDuration属性来获取秒值。
它的范围在 0 到 86,340 秒之间。
let seconds = pickerView.countDownDuration
对于您所需的情况,您有很多选择。其中2个是:
更轻松
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
}
更清洁的方式
您可以为选择器选择创建一个小型 VC,然后您可以更好地分离关注点:
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)
}
}
推荐阅读
- openxml - 显示/交互/可见 ClosedXML Excel.XLWorkbook 不保存
- java - hibernate : 如果不为空,则绑定参数
- php - 如何为查询选择的每个结果插入一个新行
- react-native - IOS 12.02 中的 react-native 可触摸不透明度点击问题
- wso2 - WSO2 5.3.0 - 从 Azure 进行联合身份验证后,多因素身份验证不起作用
- php - Mpdf 使用 pdf 模板创建新的 pdf 文档
- json - 如何在颤动中解析地图Json列表?
- node.js - Loopback4 挑战
- xpath - 如何从 https://fullcalendar.io/docs/date-clicking-selecting-demo 周视图中选择时间范围?
- java - 如何同时创建具有自动装配和非自动装配字段的构造函数?