我在swift 3中使用以下代码启动日期选择器。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var eventStartText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
eventStartText.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: TextField Delegate
func datePickerChanged(sender: UIDatePicker) {
let formatter = DateFormatter()
//formatter.dateStyle = .full
formatter.dateStyle = .long
formatter.timeStyle = .none
eventStartText.text = formatter.string(from: sender.date)
print("Try this at home")
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(datePickerChanged(sender:)), for: .valueChanged)
print("This Worked")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
eventStartText.resignFirstResponder()
return true
}
// MARK: Helper Methods
func closekeyboard() {
self.view.endEditing(true)
}
// MARK: Touch Events
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
closekeyboard()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我将如何只拥有日月和年。
而且由于要求我提供更多详细信息然后使事情变得清楚,所以我需要的是快速3日期选择器仅显示日,月,年
谢谢
最佳答案
如果只想用选择器显示日,月和年,则只需将datePickerMode
的UIDatePicker
属性设置为.date
let datePicker = UIDatePicker()
//set datePickerMode to date
datePicker.datePickerMode = .date
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(datePickerChanged(sender:)), for: .valueChanged)
关于swift3 - 快速3日期选择器仅显示日,月,年,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43982267/