Привет, разработчики. Я пытаюсь получить дату и время от UIDatePicker. Мой 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!
, поэтому, когда пользователь нажимает OK кнопку, которую он печатает:
print("dia: \(day), mes: \(month), año: \(year), hora: \(hour), minuto: \(minute)")
Печать показывает текущий день, месяц, год, часы и минуты, которые я ожидаю получить, что пользователь выбрал из UIDatePicker