使用 UIDatePicker 而不是键盘来处理 UITextField 问题
Using a UIDatePicker instead of Keyboard for UITextField Issues
你好我正在尝试在点击 UITextField
时显示 UIDatePicker
而不是键盘,以便用户可以给出事件的开始和结束时间。我的代码有效但效果不佳,而且不是我想要的方式。
我的第一个问题是,每当您点击其中一个 UITextField 时,UIDatePicker
只会在第二次点击 UITextField
时出现。否则显示原始键盘。
我的第二个问题是,每当编辑 UIDatePicker
时,只要编辑 UIDatePicker
,它只会更改 UITextField
。我需要 UITextField
在 UIDatePicker
打开后立即显示字符串,以便 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)
}
谁能帮帮我?谢谢
为什么要按动作换键盘!
只需在 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
你好我正在尝试在点击 UITextField
时显示 UIDatePicker
而不是键盘,以便用户可以给出事件的开始和结束时间。我的代码有效但效果不佳,而且不是我想要的方式。
我的第一个问题是,每当您点击其中一个 UITextField 时,UIDatePicker
只会在第二次点击 UITextField
时出现。否则显示原始键盘。
我的第二个问题是,每当编辑 UIDatePicker
时,只要编辑 UIDatePicker
,它只会更改 UITextField
。我需要 UITextField
在 UIDatePicker
打开后立即显示字符串,以便 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)
}
谁能帮帮我?谢谢
为什么要按动作换键盘!
只需在 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