我正在尝试将UIDatePicker设置为仅提供时间。按下文本字段时,DatePicker会通过操作启动。我没有使用DatePicker Viewer。任何帮助表示赞赏。感谢
let starttimedatePicker = UIDatePicker()
startTimeDiveTextField.inputView = starttimedatePicker
starttimedatePicker.addTarget(self, action: "startTimeDiveChanged:", forControlEvents: .ValueChanged)
func startTimeDiveChanged(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.timeStyle = .ShortStyle
startTimeDiveTextField.text = formatter.stringFromDate(sender.date)
}
答案 0 :(得分:13)
尝试设置其模式:
starttimedatePicker.datePickerMode = UIDatePickerMode.Time
答案 1 :(得分:4)
这可能会对你有帮助。
let starttimedatePicker = UIDatePicker()
starttimedatePicker.datePickerMode = UIDatePickerMode.Time
startTimeDiveTextField.inputView = starttimedatePicker
starttimedatePicker.addTarget(self, action: #selector(ViewController.startTimeDiveChanged), forControlEvents: .ValueChanged)
func startTimeDiveChanged(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.timeStyle = .ShortStyle
startTimeDiveTextField.text = formatter.stringFromDate(sender.date)
}
如果您在Storyboard或Xib中使用DatePicker,则按此设置。