swift - How to know when iOS UIDatePicker is dismissed? - Stack Overflow

I am using UIDatePickerStylepact to show a compact picker for selecting time:let picker = UIDatePicker

I am using UIDatePickerStylepact to show a compact picker for selecting time:

let picker = UIDatePicker()
picker.preferredDatePickerStyle = pact
picker.datePickerMode = .time

picker.addTarget(self, action: #selector(startTimeChanged(sender:)), for: .valueChanged)

@objc func startTimeChanged(sender: UIDatePicker) {
    //do something
}

This results in below picker:

This works well and the .valueChanged lets me know when the time selection is changed.

However, is there also a way of knowing when the picker is dismissed (typically by tapping outside the picker view)? I tried using .touchUpOutside but it isn't triggered when picker is dismissed. However, if I use .allEvents, then my selector is called multiple times, including when dismissed, but I have no idea which event caused it.

I am using UIDatePickerStylepact to show a compact picker for selecting time:

let picker = UIDatePicker()
picker.preferredDatePickerStyle = pact
picker.datePickerMode = .time

picker.addTarget(self, action: #selector(startTimeChanged(sender:)), for: .valueChanged)

@objc func startTimeChanged(sender: UIDatePicker) {
    //do something
}

This results in below picker:

This works well and the .valueChanged lets me know when the time selection is changed.

However, is there also a way of knowing when the picker is dismissed (typically by tapping outside the picker view)? I tried using .touchUpOutside but it isn't triggered when picker is dismissed. However, if I use .allEvents, then my selector is called multiple times, including when dismissed, but I have no idea which event caused it.

Share Improve this question edited Mar 25 at 17:35 sudoExclamationExclamation asked Mar 25 at 17:22 sudoExclamationExclamationsudoExclamationExclamation 8,82610 gold badges51 silver badges120 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I figured it out. The event .editingDidEnd calls my selector when the picker is dismissed.

picker.addTarget(self, action: #selector(dismissed(sender:)), for: .editingDidEnd)

@objc func dismissed(sender: UIDatePicker) {
    //do rest
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744179502a4561914.html

相关推荐

  • swift - How to know when iOS UIDatePicker is dismissed? - Stack Overflow

    I am using UIDatePickerStylepact to show a compact picker for selecting time:let picker = UIDatePicker

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信