7 Replies Latest reply: Sep 10, 2012 9:10 AM by etresoft
Silberlicht Level 1 Level 1 (0 points)

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 (25,645 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.

  • Silberlicht Level 1 Level 1 (0 points)

    So Cocoa target-action pattern, accepts only selectors with zero or one parameter for action message?

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

  • etresoft Level 7 Level 7 (25,645 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?

  • Silberlicht Level 1 Level 1 (0 points)

    Because besides the caller, I could need some other event information like in C# event handling, where I have the source of the event and an object deriving from EventArgs.

  • etresoft Level 7 Level 7 (25,645 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.

  • Silberlicht Level 1 Level 1 (0 points)

    I have just started to program in Objective-C and Cocoa.

    I just noticed that all the examples use one parameter when setting action selector.

    So I tried to understand why.

    And if the target-action pattern is meant for a user interface where you have zero or one parameters to send, and that is hoe things work in Cocoa, then there is no more reasons to discuss the subject.

     

    Thank you.

  • etresoft Level 7 Level 7 (25,645 points)

    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.