• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Setting action: with a variable
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting action: with a variable


  • Subject: Re: Setting action: with a variable
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Tue, 27 May 2008 20:23:30 +0200


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 (email@hidden)

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:
This email sent to email@hidden


References: 
 >Re: Setting action: with a variable (From: Gorazd Krosl <email@hidden>)

  • Prev by Date: Re: Setting action: with a variable
  • Next by Date: Re: running an external app
  • Previous by thread: Re: Setting action: with a variable
  • Next by thread: Distributed Objects/NSConnection dying under 10.4
  • Index(es):
    • Date
    • Thread