ios - 使用取消和保存按钮操作将 Swift DatePicker 显示到 UIAlertController

标签 ios swift

我的场景,我试图在 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/

相关文章:

arrays - 如何根据与特定键的相似性对字符串数组进行排序

ios - AVCaptureDevice 设备为空

ios - iOS 上的 Realm 手动安装问题

ios - 如何在 swift 4 中的自定义 UIImagePickerController 中设置按钮。

ios - 如何以编程方式填写网络表单以获取过去的身份验证页面? (在 iOS 中)

ios - 重叠的圆形按钮和点击/点击测试

ios - iOS 14.0 Beta 中反复调用 DispatchQueue.main.asyncAfter

ios - 多选问题并在 UITableview 中快速选择全部

Swift + Alamofire - 验证并获取 XML 文件

ios - 如何使用 CoreData 信息对 UITableView 进行排序