Question

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

Was it helpful?

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.

OTHER TIPS

What i'd do is to add a tag to the button myButton and set it to 1. Then in the function myFunction id do like this:

-(void)myFunction:(UIButton *)button {
    NSLog(@"%i", button.tag);                // will output 1
}

So the selector should look like this: @selector(myFunction:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top