-2

Ok so Objective-C: Calling selectors with multiple arguments works but how can I do this with addTarget:action:forControlEvents: for a UIButton? There isn't one that has withObject: I don't think... What do I do?

Community
  • 1
  • 1
Jason Silberman
  • 2,471
  • 6
  • 29
  • 47

2 Answers2

1

If you want to pass multiple arugment for UIButton you should pass a Dictionary arugment

there are 2 ways to handle it:

  1. You can use an instance variable to hold the value which you want to use.

for example :

- (void)buttonPressed:(id)sender
{
    NSMutableDictionary *argDictionary = _ivar;
}

2.You can use a category to fake a property.

how to fake a property in category , you can see : http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/

and then you can use :

- (void)buttonPressed:(UIButton *)sender
{
    // use sender.argDictionary to get your fake property
}

More info you can see my answer before

Community
  • 1
  • 1
Guo Luchuan
  • 4,729
  • 25
  • 33
0

One possibility is to create a custom UIButton-extending interface that contains all the properties you would need to pass, set that property of the button when the button is created, and then access it after the action is called. For instance:

@interface UIButton (additions)

@property NSString * customPropertyString;

@end

Then, add the target and selector the usual way:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

And lastly, the implementation of the handler method:

- (void)buttonPressed:(id)sender
{
    UIButton * senderButton = (UIButton *)sender;
    NSLog(@"%@", senderButton.customPropertyString);
}