ios - '-[UITextField date]: unrecognized selector sent to instance 0x7f89277121c0' -


i'm learning ios development on xcode 8.3.3 , ios 10.3 using swift 3. i'm trying create account creation form field shows uidatepicker when user performs on touchup on date of birth uitextfield:

    @ibaction func showdatepicker(_ sender: uitextfield) {     let datepicker = uidatepicker()     datepicker.datepickermode = uidatepickermode.date     datepicker.maximumdate = calendar.current.date(byadding: .year, value: -16, to: date())     dateofbirth.inputview = datepicker     datepicker.addtarget(self, action: #selector(self.datepickervaluechanged), for: uicontrolevents.valuechanged) }  @objc func datepickervaluechanged(_ picker: uidatepicker) {     let dateformatter = dateformatter()     dateformatter.datestyle = dateformatter.style.medium     dateformatter.timestyle = dateformatter.style.none     dateofbirth.text = dateformatter.string(from: picker.date) } 

when execute above, error:

terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uitextfield date]: unrecognized selector sent instance 0x7f89277121c0' 

to me looks trying inject uitextfield 'sender' value datepickervaluechanged field, supposed take value of type uidatepicker, causing error. tried change explicitly passed datepicker object datepickervaluechanged method:

        datepicker.addtarget(self, action: #selector(self.datepickervaluechanged(datepicker)), for: uicontrolevents.valuechanged) 

but xcode gives me error:

argument of '#selector' not refer '@objc' method, property, or initializer 

my function has @objc in front of it, i'm confused why not allowing that.

so, ultimate question is: how make sure value passed datepickervaluechanged of type uidatepicker, i.e., datepicker object?

actually, built test project, , code working fine (see below). sure function datepickervaluechanged in same class? createanaccountpage?

class viewcontroller: uiviewcontroller, uitextfielddelegate { var dateofbirth:uitextfield!  override func viewdidload() {     super.viewdidload()     dateofbirth = uitextfield(frame: cgrect(x: 0, y: 300, width: 150, height: 75))     dateofbirth.borderstyle  = .line     view.addsubview(dateofbirth)     self.showdatepicker(dateofbirth)     // additional setup after loading view, typically nib. }   func showdatepicker(_ sender: uitextfield) {     let datepicker = uidatepicker()     datepicker.datepickermode = uidatepickermode.date     datepicker.maximumdate = calendar.current.date(byadding: .year, value: -16, to: date())     dateofbirth.inputview = datepicker     datepicker.addtarget(self, action: #selector(self.datepickervaluechanged), for: uicontrolevents.valuechanged)     view.addsubview(datepicker) }  @objc func datepickervaluechanged(_ picker: uidatepicker) {     let dateformatter = dateformatter()     dateformatter.datestyle = dateformatter.style.medium     dateformatter.timestyle = dateformatter.style.none     dateofbirth.text = dateformatter.string(from: picker.date) } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -