How to declare a method used for NSMenuItem with no existing classes?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to create a typical simple macOS menu for my otherwise pure C project and this is the only Objective-C code I have in it, I didn't create any classes or anything:



#import <Cocoa/Cocoa.h>

- (void)method_new_file:(id)sender { new_flag = 1; }

void mac_menu_file_init()
{
NSMenu *currentMenu;
NSMenuItem *menuItem;

currentMenu = [[NSMenu alloc] initWithTitle:@"File"]; // set menu name

[currentMenu addItemWithTitle:@"New..." action:@selector(method_new_file:) keyEquivalent:@"n"];

// Put menu into the menubar and give up our references to the objects
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[menuItem setSubmenu:currentMenu];
[[NSApp mainMenu] insertItem:menuItem atIndex:1];
[currentMenu release];
[menuItem release];
}


The problem is obvious, I'm "missing a context for method declaration" for method_new_file, but where do I go from here? I just want that method to be called whenever I click the menu entry, but for that to happen it must be part of something, but what? Based on code and answers I've seen I tried the following:



@interface My_Actions : NSApplication
@end

@implementation My_Actions

- (void)method_new_file:(id)sender { new_flag = 1; }

@end


and other things like it, to no avail, the menu entry remains hopelessly grayed out and I don't know what else to try.










share|improve this question

























  • If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

    – Ken Thomases
    Nov 26 '18 at 20:18











  • I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

    – Michel Rouzic
    Nov 26 '18 at 20:34











  • why dont u convert the ObjC code to C instead?

    – GeneCode
    Nov 27 '18 at 0:03











  • @GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

    – Michel Rouzic
    Nov 27 '18 at 0:34


















1















I'm trying to create a typical simple macOS menu for my otherwise pure C project and this is the only Objective-C code I have in it, I didn't create any classes or anything:



#import <Cocoa/Cocoa.h>

- (void)method_new_file:(id)sender { new_flag = 1; }

void mac_menu_file_init()
{
NSMenu *currentMenu;
NSMenuItem *menuItem;

currentMenu = [[NSMenu alloc] initWithTitle:@"File"]; // set menu name

[currentMenu addItemWithTitle:@"New..." action:@selector(method_new_file:) keyEquivalent:@"n"];

// Put menu into the menubar and give up our references to the objects
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[menuItem setSubmenu:currentMenu];
[[NSApp mainMenu] insertItem:menuItem atIndex:1];
[currentMenu release];
[menuItem release];
}


The problem is obvious, I'm "missing a context for method declaration" for method_new_file, but where do I go from here? I just want that method to be called whenever I click the menu entry, but for that to happen it must be part of something, but what? Based on code and answers I've seen I tried the following:



@interface My_Actions : NSApplication
@end

@implementation My_Actions

- (void)method_new_file:(id)sender { new_flag = 1; }

@end


and other things like it, to no avail, the menu entry remains hopelessly grayed out and I don't know what else to try.










share|improve this question

























  • If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

    – Ken Thomases
    Nov 26 '18 at 20:18











  • I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

    – Michel Rouzic
    Nov 26 '18 at 20:34











  • why dont u convert the ObjC code to C instead?

    – GeneCode
    Nov 27 '18 at 0:03











  • @GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

    – Michel Rouzic
    Nov 27 '18 at 0:34














1












1








1








I'm trying to create a typical simple macOS menu for my otherwise pure C project and this is the only Objective-C code I have in it, I didn't create any classes or anything:



#import <Cocoa/Cocoa.h>

- (void)method_new_file:(id)sender { new_flag = 1; }

void mac_menu_file_init()
{
NSMenu *currentMenu;
NSMenuItem *menuItem;

currentMenu = [[NSMenu alloc] initWithTitle:@"File"]; // set menu name

[currentMenu addItemWithTitle:@"New..." action:@selector(method_new_file:) keyEquivalent:@"n"];

// Put menu into the menubar and give up our references to the objects
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[menuItem setSubmenu:currentMenu];
[[NSApp mainMenu] insertItem:menuItem atIndex:1];
[currentMenu release];
[menuItem release];
}


The problem is obvious, I'm "missing a context for method declaration" for method_new_file, but where do I go from here? I just want that method to be called whenever I click the menu entry, but for that to happen it must be part of something, but what? Based on code and answers I've seen I tried the following:



@interface My_Actions : NSApplication
@end

@implementation My_Actions

- (void)method_new_file:(id)sender { new_flag = 1; }

@end


and other things like it, to no avail, the menu entry remains hopelessly grayed out and I don't know what else to try.










share|improve this question
















I'm trying to create a typical simple macOS menu for my otherwise pure C project and this is the only Objective-C code I have in it, I didn't create any classes or anything:



#import <Cocoa/Cocoa.h>

- (void)method_new_file:(id)sender { new_flag = 1; }

void mac_menu_file_init()
{
NSMenu *currentMenu;
NSMenuItem *menuItem;

currentMenu = [[NSMenu alloc] initWithTitle:@"File"]; // set menu name

[currentMenu addItemWithTitle:@"New..." action:@selector(method_new_file:) keyEquivalent:@"n"];

// Put menu into the menubar and give up our references to the objects
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[menuItem setSubmenu:currentMenu];
[[NSApp mainMenu] insertItem:menuItem atIndex:1];
[currentMenu release];
[menuItem release];
}


The problem is obvious, I'm "missing a context for method declaration" for method_new_file, but where do I go from here? I just want that method to be called whenever I click the menu entry, but for that to happen it must be part of something, but what? Based on code and answers I've seen I tried the following:



@interface My_Actions : NSApplication
@end

@implementation My_Actions

- (void)method_new_file:(id)sender { new_flag = 1; }

@end


and other things like it, to no avail, the menu entry remains hopelessly grayed out and I don't know what else to try.







objective-c cocoa sdl-2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:04









Mark Szymczyk

14.3k24455




14.3k24455










asked Nov 26 '18 at 19:25









Michel RouzicMichel Rouzic

391218




391218













  • If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

    – Ken Thomases
    Nov 26 '18 at 20:18











  • I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

    – Michel Rouzic
    Nov 26 '18 at 20:34











  • why dont u convert the ObjC code to C instead?

    – GeneCode
    Nov 27 '18 at 0:03











  • @GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

    – Michel Rouzic
    Nov 27 '18 at 0:34



















  • If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

    – Ken Thomases
    Nov 26 '18 at 20:18











  • I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

    – Michel Rouzic
    Nov 26 '18 at 20:34











  • why dont u convert the ObjC code to C instead?

    – GeneCode
    Nov 27 '18 at 0:03











  • @GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

    – Michel Rouzic
    Nov 27 '18 at 0:34

















If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

– Ken Thomases
Nov 26 '18 at 20:18





If your code is "otherwise pure C", do you have an application object? Are you running the main event loop? Are you using some C-based GUI wrapper toolkit?

– Ken Thomases
Nov 26 '18 at 20:18













I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

– Michel Rouzic
Nov 26 '18 at 20:34





I use SDL2, other than that I didn't define any application object (SDL2 might have). So it's just a typical SDL2 program and at the beginning of the execution I run the function posted above. It seems that this is what SDL2 does in that regard: github.com/spurious/SDL-mirror/blob/…

– Michel Rouzic
Nov 26 '18 at 20:34













why dont u convert the ObjC code to C instead?

– GeneCode
Nov 27 '18 at 0:03





why dont u convert the ObjC code to C instead?

– GeneCode
Nov 27 '18 at 0:03













@GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

– Michel Rouzic
Nov 27 '18 at 0:34





@GeneCode I'd love to but how? Isn't Objective C or Swift required for Cocoa?

– Michel Rouzic
Nov 27 '18 at 0:34












1 Answer
1






active

oldest

votes


















1














Well, I would hope that SDL2 would provide a mechanism for adding menu items.



If it doesn't, you need an object to target with your menu item. In Objective-C, classes are also objects (instances of their meta-class) and they're conveniently static in lifetime. So, you could so something like:



@interface MyMenuTarget : NSObject
@end

@implementation MyMenuTarget
+ (void) openNewFile:(id)sender
{
new_flag = 1;
}
@end


Notice the + on the method declaration. That means the method is a class method, not an instance method. But a class method is just a method of the class object (compared to an instance method which is a method of an instance of the class).



And then, where you create the menu item, you should set the target to this class:



menuItem = [currentMenu addItemWithTitle:@"New..." action:@selector(openNewFile:)  keyEquivalent:@"n"];
menuItem.target = [MyMenuTarget class];





share|improve this answer
























  • Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

    – Michel Rouzic
    Nov 27 '18 at 7:55












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487753%2fhow-to-declare-a-method-used-for-nsmenuitem-with-no-existing-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Well, I would hope that SDL2 would provide a mechanism for adding menu items.



If it doesn't, you need an object to target with your menu item. In Objective-C, classes are also objects (instances of their meta-class) and they're conveniently static in lifetime. So, you could so something like:



@interface MyMenuTarget : NSObject
@end

@implementation MyMenuTarget
+ (void) openNewFile:(id)sender
{
new_flag = 1;
}
@end


Notice the + on the method declaration. That means the method is a class method, not an instance method. But a class method is just a method of the class object (compared to an instance method which is a method of an instance of the class).



And then, where you create the menu item, you should set the target to this class:



menuItem = [currentMenu addItemWithTitle:@"New..." action:@selector(openNewFile:)  keyEquivalent:@"n"];
menuItem.target = [MyMenuTarget class];





share|improve this answer
























  • Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

    – Michel Rouzic
    Nov 27 '18 at 7:55
















1














Well, I would hope that SDL2 would provide a mechanism for adding menu items.



If it doesn't, you need an object to target with your menu item. In Objective-C, classes are also objects (instances of their meta-class) and they're conveniently static in lifetime. So, you could so something like:



@interface MyMenuTarget : NSObject
@end

@implementation MyMenuTarget
+ (void) openNewFile:(id)sender
{
new_flag = 1;
}
@end


Notice the + on the method declaration. That means the method is a class method, not an instance method. But a class method is just a method of the class object (compared to an instance method which is a method of an instance of the class).



And then, where you create the menu item, you should set the target to this class:



menuItem = [currentMenu addItemWithTitle:@"New..." action:@selector(openNewFile:)  keyEquivalent:@"n"];
menuItem.target = [MyMenuTarget class];





share|improve this answer
























  • Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

    – Michel Rouzic
    Nov 27 '18 at 7:55














1












1








1







Well, I would hope that SDL2 would provide a mechanism for adding menu items.



If it doesn't, you need an object to target with your menu item. In Objective-C, classes are also objects (instances of their meta-class) and they're conveniently static in lifetime. So, you could so something like:



@interface MyMenuTarget : NSObject
@end

@implementation MyMenuTarget
+ (void) openNewFile:(id)sender
{
new_flag = 1;
}
@end


Notice the + on the method declaration. That means the method is a class method, not an instance method. But a class method is just a method of the class object (compared to an instance method which is a method of an instance of the class).



And then, where you create the menu item, you should set the target to this class:



menuItem = [currentMenu addItemWithTitle:@"New..." action:@selector(openNewFile:)  keyEquivalent:@"n"];
menuItem.target = [MyMenuTarget class];





share|improve this answer













Well, I would hope that SDL2 would provide a mechanism for adding menu items.



If it doesn't, you need an object to target with your menu item. In Objective-C, classes are also objects (instances of their meta-class) and they're conveniently static in lifetime. So, you could so something like:



@interface MyMenuTarget : NSObject
@end

@implementation MyMenuTarget
+ (void) openNewFile:(id)sender
{
new_flag = 1;
}
@end


Notice the + on the method declaration. That means the method is a class method, not an instance method. But a class method is just a method of the class object (compared to an instance method which is a method of an instance of the class).



And then, where you create the menu item, you should set the target to this class:



menuItem = [currentMenu addItemWithTitle:@"New..." action:@selector(openNewFile:)  keyEquivalent:@"n"];
menuItem.target = [MyMenuTarget class];






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 22:18









Ken ThomasesKen Thomases

71.8k673110




71.8k673110













  • Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

    – Michel Rouzic
    Nov 27 '18 at 7:55



















  • Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

    – Michel Rouzic
    Nov 27 '18 at 7:55

















Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

– Michel Rouzic
Nov 27 '18 at 7:55





Thank you, it works! And sadly no, SDL provides no way of creating menus, which seems like a rather big oversight.

– Michel Rouzic
Nov 27 '18 at 7:55




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487753%2fhow-to-declare-a-method-used-for-nsmenuitem-with-no-existing-classes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft