0

When the method that I am trying to link to my UIButton only has one parameter, then I am able to call addTarget and my code runs successfully when the button is clicked --

[ myDetailButton addTarget:self action:@selector(hideMap:) forControlEvents:UIControlEventTouchUpInside];

(void)hideMap:(NSMutableArray*)arguments

but if I add a second parameter to my hideMap method, I get an unrecognized selector error when calling it:

[ myDetailButton addTarget:self action:@selector(hideMap:) forControlEvents:UIControlEventTouchUpInside];

(void)hideMap:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options

I see this error no matter how I format the addTarget parameters, per this question --

action:@selector(hideMap)
action:@selector(hideMap:)
action:@selector(hideMap:event:)

How can I call a method that has multiple parameters using addTarget?

Community
  • 1
  • 1
storm_m2138
  • 2,281
  • 2
  • 20
  • 18

2 Answers2

2

You're problem is you are giving a selector that is expecting an NSMutableArray as the input along with a dictionary. the UIButton does not agree with this. As per your post you linked to, a UIBUtton only agrees to sending either nothing, itself as an id, or itself and an event.

I'm not 100% sure how you are expecting to get an NSMutableArray and an NSMutableDictionary from a UIButton callback, what is it exactly you are trying to achieve here, I might be able to suggest some help.

Bergasms
  • 2,203
  • 1
  • 18
  • 20
  • So you are saying that what I am trying to do -- call a method that has multiple parameters -- is not possible using UIButton's addTarget? Would the best way to do this then be to add an intermediate method that will call the action method with default parameters? – storm_m2138 Nov 14 '12 at 00:32
  • 2
    You can have either 0, 1, or 2 parameters for the selector used in `addTarget`. But you don't get to choose the parameters. They are fixed. The sender (button) is always the first and the event is always the second. Remember, it's the framework calling your method when the control event happens. It has no way to know to pass some arbitrary data that you want. It's simply saying "the event happened" and to what control. You must then use that to call any other methods you want with any other arguments you need. – rmaddy Nov 14 '12 at 00:36
  • +1 maddy, a good response. @storm_m2138 adding an intermediate method is what you want. eg button -> button callback -> your custom method – Bergasms Nov 14 '12 at 01:18
1

The issue is your method is:

-(void)hideMap:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options

And the method name is hideMap:withDict: not hideMap:event:.

Change this code action:@selector(hideMap:event:) to action:@selector(hideMap:withDict:) it'll resolve your issue.

There is no method like: hideMap:event: that is the issue.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200