7

In swift, I have a textField programmatically declared.

override func viewDidLoad() {
    super.viewDidLoad()
textField3.delegate = self

    textField3.returnKeyType = UIReturnKeyType.Next;
    datePicker.datePickerMode = UIDatePickerMode.Date
    datePicker.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
    textField3.inputView = datePicker


}

 func handleDatePicker(sender: UIDatePicker) {

    textField3.text = dateFormatter.stringFromDate(sender.date)
            }

All the other questions I've found similar to this deal with interface builder, and haven't helped. How would i fix this programmatically?

YichenBman
  • 5,011
  • 8
  • 46
  • 65
  • 1
    Have you tried creating a DatePicker and TextField separately, setting the TextField's inputView to the DatePicker, setting the DatePicker's delegate to self, and updating the TextField text in the didSelectRow method? – Ryan Mar 05 '15 at 01:03
  • i'm sorry, i didn't quite understand what you meant. I wasn't aware DatePickers had delegates, and i'm not sure of any didSelectRow method available for uitextfields – YichenBman Mar 05 '15 at 01:08
  • http://stackoverflow.com/a/27367185/2303865 – Leo Dabus Mar 05 '15 at 01:08
  • @LeonardoSavioDabus I noticed that there is an IBAction in the DatePicker that is called when editing Changed. How would I call a similar function without ibaction. Thanks – YichenBman Mar 05 '15 at 01:18
  • Whats wrong with IBAction? Just call your method from the IBAction – Leo Dabus Mar 05 '15 at 01:22
  • I don't have a storyboard/xib file. This is all programmatic, therefore I don't know how to link an ibaction to a programmatically declared datepicker @LeonardoSavioDabus – YichenBman Mar 05 '15 at 01:23
  • @YichenBman When you create it programmatically you don't need an outlet. The selector method doesn't have to be connected to an IBAction. Try to do as I suggested in my answer and let me know if you need further help. It should be pretty simple. – Leo Dabus Mar 05 '15 at 02:08

3 Answers3

15

Xcode 9 • Swift 4

@objc func handleDatePicker(_ datePicker: UIDatePicker) {
    textField3.text = datePicker.date.formatted
}

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker.addTarget(self, action: #selector(handleDatePicker), forControlEvents: .valueChanged)
}

extension Date {
    static let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "EEEE, dd MMM yyyy HH:mm:ss Z"
        return formatter
    }()
    var formatted: String {
        return Date.formatter.string(from: self)
    }
}
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • currently I have everything set up the way you put it, however I changed the function so that rather than change `textField3.text` I just `println("hello")`. No print to the logs, and its not getting called when I place breakpoints on it. I changed `UIControlEvents.ValueChanged` to `UIControlEvents.AllEvents` but it still doesn't print anything to the logs and breakpoints show that this function is still not being called. It doesn't have any parameters, and I've removed the `:` so that the selector doesn't think I'm calling a function with parameters. thanks so much for your help. – YichenBman Mar 05 '15 at 03:14
3

Swift 3 version of @Leo Dabus answer:

func handleDatePicker(sender: UIDatePicker) {
        textField3.text = datePicker.date.formatted
    }

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker.addTarget(self, action: #selector(handleDatePicker(sender:)), for: UIControlEvents.valueChanged)
}

extension Date {
    var formatted: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "EEEE, dd MMM yyyy HH:mm:ss Z"
        return  formatter.string(from: self as Date)
    }
}
Marcos Reboucas
  • 3,409
  • 1
  • 29
  • 35
0

In, Swift 4
confirm to UITableViewDelegate
make txtFChequeDate.delegate = self

func textFieldDidBeginEditing(_ textField: UITextField) {

    let datePickerView = UIDatePicker()
    datePickerView.datePickerMode = .date
    txtFChequeDate.inputView = datePickerView
    datePickerView.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)

    if txtFChequeDate.text == "" {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd MMM yyyy"
        txtFChequeDate.text = dateFormatter.string(from: Date())
    }
}

@objc func handleDatePicker(sender: UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy"
    txtFChequeDate.text = dateFormatter.string(from: sender.date)
}
Ariven Nadar
  • 1,278
  • 13
  • 13