Adding arguments to selector in addTarget
I'm having trouble adding arguments to the selector of a button (programmatically created). I've looked around the internet and tried some things, but I can't figure it out.
I create a button with the following line:
NSString *someThing = [[NSString alloc] initWithString:@"someThing"];
int counter = 4;
[anotherButton addTarget:self action:@selector(alertPressed:) forControlEvents开发者_StackOverflow:UIControlEventTouchUpInside];
I've got the function alertPressed
:
-(void)alertPressed:(id)sender {
}
How can I transfer those two variables to alertPressed
?
You can subclass UIButton
, with a custom button that contains those attributes. Then, your (id)sender
can be cast to your custom button and you can obtain the set values.
Why not global variables or properties in your class?
精彩评论