我的场景,我试图在 UIAlertController
中显示 TimePicker(仅显示 HH:MM 和 AM 或 PM),并带有取消和保存按钮操作。我试过下面的代码,但它不工作。
let myDatePicker: UIDatePicker = UIDatePicker()
myDatePicker.timeZone = NSTimeZone.local
myDatePicker.frame = CGRect(x: 0, y: 15, width: 270, height: 200)
let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.alert)
alertController.view.addSubview(myDatePicker)
let selectAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { _ in
print("Selected Date: \(myDatePicker.date)")
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
alertController.addAction(selectAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion:{})
最佳答案
通过使用下面的行修复了这个问题
myDatePicker.datePickerMode = .time
关于ios - 使用取消和保存按钮操作将 Swift DatePicker 显示到 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303838/