Search code examples
iosobjective-cselectorhardcode

iOS - Hard Code value in selector for UIButton action


I want to hard code a value inside the @selector of a UIButton's action.

When adding a value inside the @selector I get an error saying: Error

This is how you normally add a @selector to a UIButton:

[myButton addTarget:self action:@selector(myFunction::) forControlEvents:UIControlEventTouchDown];

This is what I want to do:

[myButton addTarget:self action:@selector(myFunction:1:) forControlEvents:UIControlEventTouchDown];

How can this be done?

EDIT: rmaddy pointed out a nice answer here: perfomSelector on button and sending parameters


Solution

  • You can't do that. In the abstract, a selector is just a string representing the name of the method you want to have called. In this case, the framework decides what to pass to it.

    What are you trying to accomplish with this? Depending upon the answer, you might be able to use the object reference (myButton) itself for comparison in your selector method, or you may be able to set a tag value inside the method on your button in place of the hardwired number.