-1

I'm using code to assign actions to my buttons when they're created. The following code DOES work without any problem:

button.addTarget(self, action: didTapDot, forControlEvents: .TouchUpInside)

button is the button object, and didTapDot is a name of a function in the same class, and that function is activated when the button is touched, so dot sign could be written on screen.

Now i'm trying to make a general code that could make later in single lines of code all the buttons which operate in very similar way. I made a function called didTap(content). Now in the same line as before, I want to replace the specific didTapDot into didTap(content).

let tapAction = "didTap("+content+")"
let tapActionSelector = NSSelectorFromString(tapAction)
button.addTarget(self, action: tapActionSelector, forControlEvents: .TouchUpInside)

I did so in the most "long and safe" way to avoid problems. the "action: " requires a NSSelector, so in order to make it I first made a String of the function I would like to send, then I turned it into NSSelector.

Now, Xcode shows still 0 errors and 0 warnings. I run the simulator. The keyboard successfully shows up. I tap on one of the created buttons - crash. I have no idea what's done wrong here :(

for the record, here's the error the console gives me after the crash:

2014-10-11 14:42:43.315 AppName[7634:561153] plugin gilad.AppName.Extension interrupted 2014-10-11 14:42:44.414 AppName[7634:561075] viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" UserInfo=0x7f900ad206b0 {Message=Service Connection Interrupted}

So, any ideas what could have gone wrong?

  • 1
    possible duplicate of [Attach parameter to button.addTarget action in Swift](http://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift) – Martin R Oct 11 '14 at 12:10
  • 2
    You *cannot* add arbitrary parameters to the action method. – Martin R Oct 11 '14 at 12:11
  • You mean the action _must_ include function that can't receive any information about who called it? How is it possible then to implement that code in a way that'll address each time a different button? – user2046387 Oct 11 '14 at 13:17
  • 1
    Did you have a look at the answer to the question that I linked to? The `buttonClicked:` method has a `sender` parameter which is the button than was clicked. – Martin R Oct 11 '14 at 14:44
  • Yeah. Still seems weird to get into that mess in the first place, but it did worked out that way. Thanks – user2046387 Oct 11 '14 at 17:34

1 Answers1

1

I assume this helps. I found your question because I get the same error when my keyboard is terminating, but I think is is unrelated. In any case, I had the same task. This is my solution:

    //didTap function
    func didTap(sender:UIButton){
        var proxy = textDocumentProxy as UITextDocumentProxy
        proxy.insertText(sender.titleForState(.Normal)!)
    }

    //how to use when you set up your keyboard
    button.setTitle(key.label,forState: .Normal)
    button.addTarget(self, action: "didTap:", forControlEvents: .TouchUpInside)

Please notice the ":" at the end of the function name in addTarget. It tells the underlying framework that there is a parameter expected (the sender).

Hope it helps.

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35