Skip navigation

setting selector in target-action design pattern

587 Views 7 Replies Latest reply: Sep 10, 2012 9:10 AM by etresoft RSS
Silberlicht Calculating status...
Currently Being Moderated
Sep 9, 2012 5:02 AM

I'm trying to figure out how cocoa target-action design pattern function. So, all examples I've found set action to a selector with a single parameter, which, I suppose, is the sender of the action message and is initialized in button's event handler

 

 

[myButtonCell setAction:@selector(reapTheWind:)];

[myButtonCell setTarget:anObject];

 

But what if a want a selector with more then one parameter like:

 

[myButtonCell setAction:@selector(reapTheWind:howManyTimes:)];

[myButtonCell setTarget:anObject];

 

In this case, how and where to set the value of the howManyTimes parameter?

 

And most importantly, how to check, by means of respondsToSelector, if I'm invoking the right method?

 

I guess I have to extend the NSButtonCell and override its click event handler?

iOS 5
  • etresoft Level 7 Level 7 (23,895 points)

    You can't. The target-selector pattern is meant for a user interface where you have zero or one parameters to send. The only parameter you might have is the caller, probably the button itself. Any other data would have to be accessible elsewhere by the target. There are a number of patterns likes this that take only zero or one arguments. In a few cases, you can use an NSInvocation or GCD block where you have more options.

  • etresoft Level 7 Level 7 (23,895 points)

    Silberlicht wrote:

     

    If I extend the NSButtonCell class, override the event handler and check logic for respondsToSelector, I will break this pattern?

    Why would you want to do that?

  • etresoft Level 7 Level 7 (23,895 points)

    You aren't using C#. You should have acess to the "sender" parameter. It is pretty unusual to need to look into the event itself for anythong other than keystrokes or option keys. You can get that if you need to but there is almost certainly a better way to accomplish what you are trying to do. Speaking of which, what might that be? It looks like you are falling into a trap of trying to code Cocoa as if it is something else. Explain what you are trying to accomplish at a higher level. Ignore the details for right now.

  • etresoft Level 7 Level 7 (23,895 points)
    Currently Being Moderated
    Sep 10, 2012 9:10 AM (in response to Silberlicht)

    Yes, I guess. It is always best to follow the vendor's architecture for building these things, even if it is unfamiliar. Otherwise, you are own your own.

     

    If you can't see how to accomplish what you want to do, then that is a different question.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.