Re: SEL and NSSelectorFromString
Re: SEL and NSSelectorFromString
- Subject: Re: SEL and NSSelectorFromString
- From: Ondra Cada <email@hidden>
- Date: Sun, 20 Jan 2002 17:57:16 +0100
Jason,
>
>>>>> Jason Blake (JB) wrote at Sat, 19 Jan 2002 20:12:57 -0600:
JB> I can't find much documentation for what SEL is,
Actually, an integer (or a pointer, which is the same).
JB> or what the format the
JB> parameter for NSSelectorFromString takes.
An NSString.
JB> @implementation AppMenuController
JB> - (void)awakeFromNib
JB> {
JB> //create menu
JB> SEL action = NSSelectorFromString(@"item1");
JB> appMenu=[[NSMenu alloc] initWithTitle:@"title"];
JB> [appMenu addItem:[ [NSMenuItem alloc] initWithTitle:@"item 1"
JB> action:action keyEquivalent:@""] ];
JB> }
JB> -(void) item1
JB> {
JB> NSLog(@"%@",@"\nItem1 selected");
JB> }
JB> When running the app "item 1" shows up and is selectable, but when I
JB> select it I get no log message. Can anyone help
Forgot to set the target. Incidentally, for cases like this there's the
@selector directive, which is much faster (being a compile-, or rather
link-time constant):
NSMenuItem *mi;
appMenu=[[NSMenu alloc] initWithTitle:@"title"];
[appMenu addItem:mi=[[NSMenuItem alloc] initWithTitle:@"item 1"
action:@selector(item1) keyEquivalent:@""]];
[mi setTarget:self];
You need the NSSelectorFromString only in case the name is generated somehow:
NSString *name=...read some config file, eg...
SEL action= NSSelectorFromString([name stringByAppendingString:@":"]);
...
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc