Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 从UIDatePicker警报获取所选日期_Swift_Uidatepicker - Fatal编程技术网

Swift 从UIDatePicker警报获取所选日期

Swift 从UIDatePicker警报获取所选日期,swift,uidatepicker,Swift,Uidatepicker,大家好,我正在尝试从UIDatePicker获取日期和时间 我的提货员收到警报了 let myDatePicker: UIDatePicker = UIDatePicker() let loc = Locale(identifier: "Es_mx") //myDatePicker.timeZone = NSTimeZone.local myDatePicker.locale = loc let date = myDatePick

大家好,我正在尝试从UIDatePicker获取日期和时间 我的提货员收到警报了

let myDatePicker: UIDatePicker = UIDatePicker()
        let loc = Locale(identifier: "Es_mx")
        //myDatePicker.timeZone = NSTimeZone.local
        myDatePicker.locale = loc

        let date = myDatePicker.date
        let components = Calendar.current.dateComponents([.hour, .minute, .year, .day, .month], from: date)
        let year = components.year!
        let day = components.day!
        let month = components.month!
        let hour = components.hour!
        let minute = components.minute!
       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 somethingAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
        let somethingAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default) { (somethingAction: UIAlertAction!) in
                       print("dia: \(day), mes: \(month), año: \(year), hora: \(hour), minuto: \(minute)")
            }


       let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
       alertController.addAction(somethingAction)
       alertController.addAction(cancelAction)
       present(alertController, animated: true, completion:{})


    }
已经尝试了

let date = myDatePicker.date
            let components = Calendar.current.dateComponents([.hour, .minute, .year, .day, .month], from: date)
            let year = components.year!
            let day = components.day!
            let month = components.month!
            let hour = components.hour!
            let minute = components.minute!
因此,当用户按下“确定”按钮时,它会打印:

print("dia: \(day), mes: \(month), año: \(year), hora: \(hour), minuto: \(minute)")
打印显示当前日期、月份、年份、小时和分钟
我希望得到用户从UIDatePicker中选择的内容,您可以初始化一个datePicker并立即从中获取日期。这就是问题所在

如果要在按下“确定”按钮时从日期选择器获取日期,则必须在结束时执行此操作。将
let date=myDatePicker.date
行移动到闭包中,如下所示:

let somethingAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default) { (somethingAction: UIAlertAction!) in
                       let date = self.myDatePicker.date
                       print("dia: \(day), mes: \(month), año: \(year), hora: \(hour), minuto: \(minute)")
            }

我不明白你的问题。你期望的结果是什么?你得到的结果是什么?谢谢@Teo已经编辑了我的问题谢谢myDatePicker。addTarget(self,action:#selector(self.cambio(sender:)),for:.valueChanged)成功了