SlangKing — UIDatePicker update

Vince Lee
Vince Lee
Aug 23, 2017 · 1 min read
var formatter: DateFormatter! = niloverride func viewDidLoad() {formatter = DateFormatter()formatter.dateFormat = "yyyy-MM-dd"let myDatePicker = UIDatePicker()myDatePicker.datePickerMode = .datemyDatePicker.date = NSDate() as Date
// 設置 UIDatePicker 改變日期時會執行動作的方法
myDatePicker.addTarget(self,action: #selector(EditProfileTableViewController.datePickerChanged),for: .valueChanged)// 將 UITextField 原先鍵盤的視圖更換成 UIDatePickerbirthdayTextField.inputView = myDatePickerbirthdayTextField.tag = 200}@objc func datePickerChanged(datePicker:UIDatePicker) {// 依據元件的 tag 取得 UITextFieldlet myTextField =self.view?.viewWithTag(200) as? UITextField// 將 UITextField 的值更新為新的日期myTextField?.text =formatter.string(from: datePicker.date)}

)

Vince Lee

Written by

Vince Lee

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade