Using a nested function as the selector of an action - swift -


this code:

func didselectlabel() {      let label = uilabel(frame: cgrect(x: 0, y: 0, width: 50, height: 30))      label.center = viewforedit.center     label.textalignment = .center     label.isuserinteractionenabled = true      func userdragged(gesture: uipangesturerecognizer) {          let loc = gesture.location(in: self.viewforedit)         label.center = loc      }      gesture = uipangesturerecognizer(target: self, action: userdragged(gesture:))     label.addgesturerecognizer(gesture)      let alert = uialertcontroller(title: "write text", message: "", preferredstyle: .alert)     let continueaction = uialertaction(title: "continue", style: .default) { (uialertaction) in          label.text = alert.textfields?[0].text      }      let cancelaction = uialertaction(title: "cancel", style: .cancel, handler: nil)     alert.addtextfield { (textfield) in          textfield.placeholder = "write here"      }      alert.addaction(continueaction)     alert.addaction(cancelaction)      present(alert, animated: true, completion: nil)      self.viewforedit.addsubview(label)  } 

i use

func userdragged(gesture: uipangesturerecognizer)

as selector of

gesture = uipangesturerecognizer(target: self, action: userdragged(gesture:))

the problem if run code crashes saying

terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[testapp.viewcontrollereditor userdragged:]: unrecognized selector sent instance 0x7fc6b3c282b0'

p.s. cannot put function 'userdragged(gesture:)' outside of function 'didselectlabel()' becuse if 'label.center = loc' returns error. because 'label' called inside of function 'didselectlabel()'

the easiest way move function outside didselectlabel, , change to:

func userdragged(gesture: uipangesturerecognizer) {     let loc = gesture.location(in: self.viewforedit)     let label = gesture.view as? uilabel     label?.center = loc  } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -