Le 27 mai 08 à 20:17, Gorazd Krosl a écrit :

Greetings,

I am having trouble with something that should be pretty
basic..

What I would like to do is set the action for NSButton
programmatically where the selector is a variable.

I can't seem to find an example.

[aButton addTarget:self action:variable ....]

If I define 'variable' to be NSString it is happy
but at run time I
get an error stating that the target selector is Null. I
can assure
you that variable exists and has a value of
'buttonAction:'.

I of course have buttonAction defined as follows:

- (void) buttonActon:(id)sender


What am I doing wrong?

-mark=

Hi Mark,
The "action" needs to be a SEL type (a selector). There is a function in Foundation: NSSelectorFromString (NSString *aSelectorName), which returns selector for <aSelectorName> if one exists. You can pass this to a -setAction: method of NSControl (of which NSButton is a subclass).

And you can also tell the compiler to generate a SEL from a method name using the @selector(myMethod:withArg:) syntax.

NSSelectorFromString documentation:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#/ /apple_ref/c/func/NSSelectorFromString

-setAction: documentation:
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#/ /apple_ref/occ/instm/NSControl/setAction:

(Note that the colon in above URL is a part of URL)

HTH
Gorazd

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to