0

In this SO question, user feihu recommends an approach with a different action as the target for the TouchUpInside event.

Here are the relevant pieces:

 [btn addTarget:self action:@selector(btnTouchUp:withEvent:) forControlEvents:UIControlEventTouchUpInside];
 (void)btnTouchUp:(UIButton *)sender withEvent:(UIEvent *)event {
   ...
}

Converting this code to Swift has been maddening.

We tried:

button.addTarget(self, action: "buttonTouchUp:", forControlEvents: .TouchUpInside)
func buttonTouchUp(sender: AnyObject, event: UIEvent) {
...
}

and

button.addTarget(self, action: "buttonTouchUp:", forControlEvents: .TouchUpInside)
func buttonTouchUp(sender: UIButton!, event: UIEvent) {
...
}

Anything other than the standard action message (with sender: UIButton! as the only parameter) generates an exception saying an unrecognized selector was sent.

Searching Apple docs for other signatures on action messages yields nothing. We checked the UIButton and UIControl classes.

We Googled UIButton action message, UIControl selector action, UIButton events, UIButton action function signature, and all sorts of variations, but nothing.

How can we convert his Obj-C code? Or where can we find all the possible actions which can be used for handling UIButton events?

Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

4

You need to change the selector your use when setting up the button:

button.addTarget(self, action: "buttonTouchUp:event:", 
  forControlEvents: .TouchUpInside)

func buttonTouchUp(sender: UIButton!, 
  event: UIEvent) {

(The action signature follows the Cbjective-C syntax, where you just list the parameters followed by colons.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Ugh, thanks so much. New to Swift so stuff like this is maddening. For future reference, what terms would you have Googled if you were like us and trying to find the answer? – Crashalot Aug 28 '15 at 22:49
  • 1
    "Swift IBAction method signature" maybe? – Duncan C Aug 28 '15 at 23:52
  • Thanks! We tried something similar (`UIButton action function signature`) but couldn't find anything. You are better than Google. :) – Crashalot Aug 28 '15 at 23:55
  • Done! Thanks again for your help! – Crashalot Aug 29 '15 at 01:10
  • After browsing your profile on CodeMentor, how long would it take you to write code that highlighted a UIButton (e.g., similar to keyboard: scale by 1.5 and translate up) on touch but reset the button once the user drags/touches out of the button hit area. – Crashalot Aug 29 '15 at 01:48
  • You mean an app that did that? Not that long. I've done similar for demo projects. – Duncan C Aug 29 '15 at 11:26
  • No, not an app. Just the functions. We tried writing it (hence, this question), but it's not working quite right and there are many other things we need to finish. – Crashalot Aug 29 '15 at 21:07
  • Not what I'm asking. Are you looking to develop a custom button class to be included in an app? There was another thread I was participating in recently where the poster wanted to add behavior to Interface builder. I wanted to make sure I understood what you were asking. – Duncan C Aug 29 '15 at 21:38
  • Oh sorry. No, standard UIButton is fine. It's part of a keyboard for an app, and we just want to highlight a key when tapped so someone can see if they're tapping the right one. Doesn't even need to be as sophisticated as the iOS keyboard: just highlight first key tapped and cancel highlighting if finger gets lifted or dragged out of hit area (i.e., doesn't need to continuously highlight new keys as you drag across keyboard). – Crashalot Aug 29 '15 at 22:28