0

I got a custom UIPickerView from github (https://github.com/bendodson/MonthYearPickerView-Swift") and now I'm trying to connect it to a textfield with no luck. I managed to do it with the standard UiDatePicker using .addTarget and .valueChanged methods, but with this custom one addTarget throws an error. Now I only manage to get the textfield's inputView to the custom PickerView, but not save the input using my "Done" button that I created. What is it that I'm missing?

lazy var ExpireDatetextfeild: UITextField = {
    let tf = LeftPaddedTextFeild()
    tf.placeholder = "MM/YY"
    tf.translatesAutoresizingMaskIntoConstraints = false
    tf.addTarget(self, action: #selector(textfeildediting), for: .editingDidBegin)
    return tf
}()


let DatePickerView: MonthYearPickerView = MonthYearPickerView()


func textfeildediting() {
    let DatePickerView: MonthYearPickerView = MonthYearPickerView()
    DatePickerView.backgroundColor = .white

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.backgroundColor = UIColor.white
    toolBar.sizeToFit()
    let donebutton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(DoneFunc))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelFunc))
    toolBar.setItems([cancelButton, spaceButton, donebutton], animated: false)
    toolBar.isUserInteractionEnabled = true

    ExpireDatetextfeild.inputAccessoryView = toolBar
    ExpireDatetextfeild.inputView = DatePickerView

}

func cancelFunc(sender: UIBarButtonItem) {
    DatePickerView.isHidden = true
    print("DatePickerPrint")
}

func DoneFunc(sender: UIBarButtonItem) {
    DatePickerView.onDateSelected = { (month: Int, year:Int) in
    let Yearstring = String(format: "%02d/%d", month, year)
    print(Yearstring)
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • https://github.com/bendodson/MonthYearPickerView-Swift The link to the github code – Jonathan Nairn Jan 28 '17 at 17:54
  • " throws an error": What error? My guess? Your selector is wrong. You didn't show in the selector for the method signature that each of them has a parameter: sender. – Larme Sep 01 '17 at 13:44
  • Possible duplicate of [Passing arguments to selector in Swift3](https://stackoverflow.com/questions/43251708/passing-arguments-to-selector-in-swift3) – Larme Sep 01 '17 at 13:44

0 Answers0