views:

63

answers:

2

I have made some code to do this, but as It doesn't work. I am only a beginner so I may be doing this completely wrong or i may have missed something out. Either way please tell me. Here is the code I have got.

[item setAction:(SEL)action {
     [window makeKeyAndOrderFront:NSApp];
    }
+3  A: 

The SEL data type refers to a selector, which is a way of referring to a method in a class. What you have to do is define a method in your target object's class:

- (void)doSomething:(id)sender {
    [window makeKeyAndOrderFront:sender];
}

and call your control's setAction: method like this:

[item setAction:@selector(doSomething:)];

It looks like you need to bone up on the basics of Cocoa, however. May I suggest Apple's Getting Started guide

Dave R
+5  A: 

It looks like you're trying to make an anonymous funtion. AFAIK, Obj-C has no support for these. Basically want you want to do is define action as a proper method:

[item setAction:@selector(action:)];

-(void)action:(id)sender{  
    [window makeKeyAndOrderFront:self];  
}

Also, I'm not sure why you're passing NSApp to makeKeyAndOrderFront:. The full signature is - (void)makeKeyAndOrderFront:(id)sender, so passing self is usually most appropriate (although I'm not even sure what that input does!).

I suggest you work through a few tutorials to familiarize yourself with Obj-C and Cocoa.

Rich Catalano
Actually, NSApp is the shared application instance, of type NSApplication.
Perspx
Oh cool, didn't know that! I'll edit my post to reflect that. Thanks! To think, all this time I've been using [NSApplication sharedApplication]!
Rich Catalano