For UIButton how to pass multiple arguments through @selector [duplicate]-Collection of common programming errors
Short answer: You don’t. The @selector
there tells the button what method to call when it gets tapped, not the arguments that it should pass to the method.
Longer answer: If you know when you’re creating the button what the argument is going to be, then you can wrap it up like this:
// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
... later ...
- (void)imageButtonTapped:(id)sender
{
[self doStuffToMyImageWithArgument:10];
}
- (void)doStuffToMyImageWithArgument:(NSInteger)argument
{
... do what you gotta do ...
If you don’t know, then you probably want to save the argument to a variable somewhere.
// In your @interface
@property (nonatomic, assign) NSInteger imageStuffArgument;
... later ...
// In loadView or viewDidLoad or wherever:
[imageButton addTarget:self action:@selector(imageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
... later ...
- (void)respondToWhateverChangesTheArgument
{
self.imageStuffArgument = 10;
}
... later ...
- (void)imageButtonTapped:(id)sender
{
[self doStuffToMyImageWithArgument:self.imageStuffArgument];
}
- (void) doStuffToMyImageWithArgument:(NSInteger)argument
{
... do what you gotta do ...