In my ViewController
there are several UiTextfield
.
I intend to use a single UIDatePicker
that is expected to respond accordingly.
my code only responds to event generated by second textfield and not the first one.
I just need to determine which UITextField
has generated the event...
import UIKit
class ViewController: UIViewController , UITextFieldDelegate {
var datePicker = UIDatePicker()
@IBOutlet weak var tvDueDate: UITextField!
@IBOutlet weak var tvOtherDate: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//setupDatePicker()
self.tvDueDate.tag = 0
self.tvOtherDate.tag = 1
//setupDatePicker(textField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidEndEditing(textField: UITextField) {
setupDatePicker(textField)
}
func textFieldDidBeginEditing(textField: UITextField) {
setupDatePicker(textField)
}
func setupDatePicker(text: UITextField) {
// Sets up the "button"
//tvDueDate.text = "Pick a due date"
//tvDueDate.textAlignment = .Center
// Removes the indicator of the UITextField
//tvDueDate.tintColor = UIColor.clearColor()
// Specifies intput type
datePicker.datePickerMode = .Date
// Creates the toolbar
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// Adds the buttons
var doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneClick")
var spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Adds the toolbar to the view
if text.tag == 0{
self.tvDueDate.inputView = datePicker
self.tvDueDate.inputAccessoryView = toolBar
}
else
{
self.tvOtherDate.inputView = datePicker
self.tvOtherDate.inputAccessoryView = toolBar
}
}
func doneClick() {
var dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.dateStyle = .ShortStyle
if tvDueDate.isFirstResponder(){
tvDueDate.text = dateFormatter.stringFromDate(datePicker.date)
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.text = dateFormatter.stringFromDate(datePicker.date)
tvOtherDate.resignFirstResponder()
}
}
func cancelClick() {
if tvDueDate.isFirstResponder(){
tvDueDate.resignFirstResponder()
}else{
tvOtherDate.resignFirstResponder()
}
}
}
Give unique tags to your UITextFields and use UITextField delegates.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//setupDatePicker()
self.tvDueDate.tag = 0
self.tvOtherDate.tag = 1
self.tvDueDate.delegate = self
self.tvOtherDate.delegate = self
//setupDatePicker(textField)
}
// UITextField Delegates
func textFieldDidBeginEditing(textField: UITextField) {
println("TextField did begin editing method called")
if textField.tag == 0
..........
if textField.tag == 2
....
}
func textFieldDidEndEditing(textField: UITextField) {
println("TextField did end editing method called")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println("TextField should begin editing method called")
return true;
}