I'm creating an app that has a UITextField on it, which, when tapped / clicked, will show a Date Picker that will flyout from the bottom. Said popup panel has a Done button, which will apply whatever date is selected (default to today) to the textfield upon click / tap, of preferably YYYY-MM-DD format.
I've been digging around the net, and so far I've seen examples with an IBAction for the textfield, with various ways as to how the done button works (and most without a done button), but I was wondering if I could recycle or base it off my code for a drop down menu below:
let wellheadPressTest : [String] = ["PSIG", "MPAG"]
var wellboreStatusPickerView = UIPickerView()
override func viewDidLoad() {
let wellboreStatusToolBar = UIToolbar()
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneButtonWellbore = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePickerWellbore")
wellboreStatusToolBar.barStyle = UIBarStyle.Default
wellboreStatusToolBar.translucent = true
wellboreStatusToolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
wellboreStatusToolBar.sizeToFit()
wellboreStatusToolBar.setItems([spaceButton,spaceButton,doneButtonWellbore], animated: false)
wellboreStatusToolBar.userInteractionEnabled = true
wellboreStatusPickerView.delegate = self
self.samplingWaterFormOneView.textfieldWellbore.inputView = wellboreStatusPickerView
self.samplingWaterFormOneView.textfieldWellbore.inputAccessoryView = wellboreStatusToolBar
}
// Various Required PickerView functions
func donePickerWellbore(){
let row = wellboreStatusPickerView.selectedRowInComponent(0);
pickerView(wellboreStatusPickerView, didSelectRow: row, inComponent:0)
self.samplingWaterFormOneView.textfieldWellbore.resignFirstResponder()
}
Here's what I tried:
var dateSampledPickerView = UIDatePicker()
override func viewDidLoad() {
let dateSampledPickerToolBar = UIToolbar()
let doneButtonDate = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePickerDate")
dateSampledPickerToolBar.barStyle = UIBarStyle.Default
dateSampledPickerToolBar.translucent = true
dateSampledPickerToolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
dateSampledPickerToolBar.sizeToFit()
dateSampledPickerToolBar.setItems([spaceButton,spaceButton,doneButtonDate], animated: false)
dateSampledPickerToolBar.userInteractionEnabled = true
dateSampledPickerView.datePickerMode = UIDatePickerMode.Date
dateSampledPickerView.addTarget(self, action: Selector("datePickerValueChanged"), forControlEvents: UIControlEvents.ValueChanged)
self.samplingWaterFormOneView.textfieldDateSampled.inputView = dateSampledPickerView
self.samplingWaterFormOneView.textfieldDateSampled.inputAccessoryView = dateSampledPickerToolBar
self.samplingWaterFormOneView.textfieldDateSampled.delegate = self
}
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = "YYYY-MM-DD"
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
self.samplingWaterFormOneView.textfieldDateSampled.text = dateFormatter.stringFromDate(sender.date)
}
func donePickerDate(){
self.samplingWaterFormOneView.textfieldDateSampled.resignFirstResponder()
}
My code above does show a pop-up menu with a date picker when I top the textfield, with a done button, but the textfield doesn't get the date.
EDIT: Also tried modifying the datePickerValueChanged such that it doesn't have a sender, and also uses the dateSampledPickerView for the textfield. Didn't work.
Putting in comments in the function suggests that the datePickerValueChanged function isn't being called.
What am I doing wrong?
Here you have an example of date picker with toolbar:
var date: NSDate?
var timePicker: UIDatePicker?
@IBOutlet weak var dateLabel: UILabel!
func initDatePicker() {
if self.timePicker == nil {
self.timePicker = UIDatePicker()
self.timePicker!.datePickerMode = UIDatePickerMode.Date
self.timePicker!.addTarget(self, action: "updateDate", forControlEvents: UIControlEvents.ValueChanged)
self.timePicker!.minimumDate = NSDate()
self.timePicker!.backgroundColor = UIColor().backgroundColor()
self.dateLabel.inputView = self.timePicker
self.dateLabel.inputAccessoryView = self.createPickerToolBar()
}
}
func updateDate() {
let picker: UIDatePicker = self.dateLabel.inputView as! UIDatePicker
self.dateLabel.text = picker.date.stringFromDate(kDateFormatSlash)
self.date = picker.date
self.delegate?.didIntroduceText(self.actionTextView.text.characters.count != 0 && self.searchableTableView?.getText().characters.count != 0 && self.dateLabel.text!.characters.count != 0)
}
func createPickerToolBar() -> UIToolbar {
let toolbar = UIToolbar()
let doneButton = UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Done, target: self, action: "doneAction")
doneButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: kOpenSansRegular, size: kHeaderFontSize)!, NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)
doneButton.accessibilityLabel = "DoneToolbar"
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolbar.translucent = false
toolbar.sizeToFit()
toolbar.setItems([spaceButton, doneButton], animated: false)
toolbar.userInteractionEnabled = true
return toolbar
}
func doneAction() {
self.updateDate()
self.dateLabel.resignFirstResponder()
self.timePicker?.removeFromSuperview()
}
I believe the datePickerValueChanged function isnt being called becasue you have not set the delegate of the dateSampledPickerView.
So simply enter
dateSampledPickerView.delegate = self
*********EDIT****************
Ok so after looking closer I believe you have set the action call incorrectly. Replace this line
dateSampledPickerView.addTarget(self, action: Selector("datePickerValueChanged"), forControlEvents: UIControlEvents.ValueChanged)
with
dateSampledPickerView.addTarget(self, action: "datePickerValueChanged", forControlEvents: UIControlEvents.ValueChanged)
removing the Selector()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With