alerts-and-pickers
alerts-and-pickers copied to clipboard
DatePicker and PickerView issues.
if picker values don't change, it will not call Action
@SoolyChristy right) You want to call Action not to changing the picker? For what?
if pick first option how i can get the value? @dillidon pick first option Action will not be called
@SoolyChristy what you mean? better attache image
@dillidon How can I get the selected date on Done button click in alertcontroller?
I am having the same issue. How can I get the selected value in picker view(Without scrolling) after presenting the alert controller for the first time?
Please suggest something
Very simple solution here.
- Make a function that returns the value or the picker itself in an extension of UIAlertController
- You can get the DatePickerViewController or PickerViewController by value(forKey: "contentViewController") function
- add an action to your alertController and in the handler revoke the values from the function. Will make a merge request for it. 😃 Example Code
private func selectDate(_ handler: @escaping ((TimeInterval) -> Void)) {
var dateTimeInterval: TimeInterval = 0
let alert = UIAlertController(style: .actionSheet)
alert.addDatePicker(mode: .date, date: nil, minimumDate: nil, maximumDate: nil) { (date) in
dateTimeInterval = date.timeIntervalSince1970
}
alert.addAction(title: "确定", style: .cancel) { (_) in
handler(dateTimeInterval)
}
present(alert, animated: true, completion: nil)
}