views:

46

answers:

2

Given that you have a method

-(void)aSelector:(id)anyArgument;

And have set up a UIButton programmatically

UIButton *myButton = [[UIButton alloc] init]  

Alright, I get the idea that when you create a UIButton, you have to use the method

[myButton addTarget:nil action@selector(aSelector:) for ControlEvents:UIControlEventTouchUpInside];

But where do I pass the argument? I know that normally, you would use

[myButton withObject:anyArgument];

But NSControl doesn't allow for that but it is possible as the colon after the selector name indicates so.

+6  A: 

Typically, -aSelector: will be defined like this

- (void) aSelector:(id)sender
{
NSLog(@" sender = %@", sender);
}

and the argument will be myButton. Bear in mind that you might have several buttons all calling the method -aSelector: and you would, then, need to distinguish which was the caller (sender) at runtime.

westsider
Correct. And just to clarify: The argument passed is the control that triggered the action. In this case, `myButton`.
Squeegy
Note also that you can set a "tag" property on individual controls, so if you have more than one control hooked up to the same action, you can determine which is which by examining the tag.
Mark Bessey
+2  A: 

addTarget of UIControl only supports these three forms of selectors.

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

you can't add custom arguments.

fluchtpunkt