我在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日期选择器仅显示日月 感谢
答案 0 :(得分:6)
如果您只想显示带有选择器的日,月和年,那么只需将datePickerMode
的{{1}}属性设置为UIDatePicker
.date