First let me state all the other code you see works as desired except for the UIDatePicker. Right now when I click on the dateTextField the UIDatePicker
shows up and I can select a date but when I click the Done button the Date selected is not in the UITextField
assigned to it.
One area that may be a place to focus is the doneClicked function. When I initially created it I did not have the (@objc) in front of it and it gave me an error.
/Argument of '#selector' refers to instance method doneClicked()
that is not exposed to Objective-C/
So I added (@objc) and the error went away but as stated above the date selected from the UIDatePicker
is not being saved in the UITextField
.
If you would please explain and show where my code is wrong and how to fix it.
Any help is greatly appreciated in this matter thank you very much.
import Foundation
import UIKit
import Firebase
import FirebaseUI
class TrainViewController: UIViewController{
@IBOutlet weak var trainField: UITextField!
@IBOutlet weak var locationField: UITextField!
@IBOutlet weak var engineField: UITextField!
@IBOutlet weak var dateTextField: UITextField!
let datePicker = UIDatePicker()
@IBAction func saveButton(_ sender: UIButton) {
self.saveText()
trainField.text?.removeAll()
locationField.text?.removeAll()
engineField.text?.removeAll()
dateTextField.text?.removeAll()
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
}
func saveText() {
let trainText = self.trainField.text!
let locationText = self.locationField.text!
let engineText = self.engineField.text!
let dateStart = self.dateTextField.text!
let db = Firestore.firestore()
let dict = ["train_text": trainText,
"location_text": locationText,
"engine_text": engineText,
"dateStart_text": dateStart]
db.collection("users").addDocument(data: dict)
}
func createDatePicker(){
//format the display of our datepicker
datePicker.datePickerMode = .date
//assign date picker to our textfield
dateTextField.inputView = datePicker
//create a toolbar
let toolbar = UIToolbar()
toolbar.sizeToFit()
//add a done button on this toolbar
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneClicked))
toolbar.setItems([doneButton], animated: true)
dateTextField.inputAccessoryView = toolbar
}
@objc func doneClicked(){
//format the date display in textfield
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
}
}
extension TrainViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
you are not assigning the date in textFiled.
Like : dateTextField.text = dateString
in doneClicked click, get the date form datePicker and assign to dateTextField.
Code:
@objc func doneClicked(){
//format the date display in textfield
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
let dateString = dateFormatter.stringFromDate(datePicker.date)
print(dateString)
dateTextField.text = dateString
}