gpt4 book ai didi

ios - 使用 UIDatePicker 而不是键盘来处理 UITextField 问题

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:26 25 4
gpt4 key购买 nike

您好,我正在尝试在点击 UITextField 时显示 UIDatePicker 而不是键盘,以便用户可以为事件指定开始和结束时间。我的代码有效但效果不佳,而且不是我想要的方式。

我的第一个问题是,每当您点击其中一个 UITextField 时,UIDatePicker 只会在第二次点击 UITextField 时出现。否则它会显示原始键盘。

我的第二个问题是,无论何时编辑 UIDatePicker,只要编辑 UIDatePicker,它只会更改 UITextField。我需要 UITextFieldUIDatePicker 打开后立即显示字符串,以便 UITextField 与正在发生的事情保持同步UIDatePicker

这是我当前的代码:

override func viewDidLoad() {
super.viewDidLoad()
var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
StartTimeTextField.inputView = datePickerView
datePickerView.addTarget(self, action:
Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

var datePickerView2 : UIDatePicker = UIDatePicker()
datePickerView2.datePickerMode = UIDatePickerMode.Time
EndTimeTextField.inputView = datePickerView2
datePickerView2.addTarget(self, action: Selector("handleDatePicker2:"), forControlEvents: UIControlEvents.ValueChanged)
}

func handleDatePicker(sender: UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh mm am pm"
StartTimeTextField.text = dateFormatter.stringFromDate(sender.date)
}



func handleDatePicker2(sender: UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh mm am pm"
EndTimeTextField.text = dateFormatter.stringFromDate(sender.date)
}

谁能帮帮我?谢谢

最佳答案

为什么你要通过 Action 来改变键盘!

只需在 viewDidLoad 中添加您的代码:

var datePickerView  : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
StartTimeTextField.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
EndTimeTextField.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker2:"), forControlEvents: UIControlEvents.ValueChanged)

并删除像

这样的方法
@IBAction func

关于ios - 使用 UIDatePicker 而不是键盘来处理 UITextField 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com