Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

setting selector in target-action design pattern

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

Posted on Sep 9, 2012 5:02 AM

Reply
Question marked as Best reply

Posted on Sep 9, 2012 5:30 AM

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.

7 replies
Question marked as Best reply

Sep 9, 2012 5:30 AM in response to Silberlicht

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.

Sep 9, 2012 9:47 AM in response to Silberlicht

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.

Sep 10, 2012 7:19 AM in response to etresoft

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.

setting selector in target-action design pattern

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.