简体   繁体   English

如何在glfw应用程序中实现NSApplicationDelegate协议

[英]How to implement NSApplicationDelegate protocol in glfw application

I am currently programming an application using glfw in MacOS X. My only problem is that the application doesn't use an AppDelegate, but does all the initialization in the main.cpp file like a command line utility. 我目前正在MacOS X中使用glfw编写应用程序。我唯一的问题是该应用程序不使用AppDelegate,而是像命令行实用程序一样在main.cpp文件中进行所有初始化。 I specifically want to implement the function 我特别想实现该功能

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

and I already registered the file extensions in the plist file and when I try to open them I get an error message saying " The document "Doc.xxx" could not be opened. MyApp cannot open files in the "xxx" format. 并且我已经在plist文件中注册了文件扩展名,当我尝试打开它们时,出现一条错误消息,提示“ 文档“ Doc.xxx”无法打开。MyApp无法打开“ xxx”格式的文件。

I attempted to create a singleton that sets itself as the delegate but it just doesn't call the function. 我试图创建一个将自己设置为委托的单例,但它只是不调用该函数。 Can anyone comment on the possibility of creating a class and implementing the NSApplicationDelegate functions? 任何人都可以评论创建类和实现NSApplicationDelegate函数的可能性吗?

Basically there is nothing in your code to receive events from the OS and to pump the events received. 基本上,您的代码中没有任何内容可以接收来自OS的事件并泵送接收到的事件。

The solution, according to another stack overflow Q/A, appears to be to invoke NSApplication manually. 根据另一个堆栈溢出问题,该解决方案似乎是手动调用NSApplication。 You still need to decide where to hook your OpenGL render loop into as [NSApp run] doesn't return until the app finishes. 您仍然需要确定将OpenGL渲染循环挂接到何处,因为[NSApp run]在应用程序完成之前不会返回。 Another solution could be to make a custom NSApplication replacement. 另一个解决方案是进行自定义NSApplication替换。 http://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html http://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html

Copied pretty much verbatim from this answer taking into account the modifications for ARC: https://stackoverflow.com/a/2154666/1874323 考虑到ARC的修改,从此答案中几乎逐字复制: https : //stackoverflow.com/a/2154666/1874323

@autoreleasepool { 
    AppDelegate * delegate = [[AppDelegate alloc] init];

    NSApplication * application = [NSApplication sharedApplication];
    [application setDelegate:delegate];
    [NSApp run]; // Note this never returns until the app finishes, so you need to decide where to hook your code into
}

As always you can find documentation from the Apple website: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/Reference/Reference.html 与往常一样,您可以从Apple网站上找到文档: https : //developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/Reference/Reference.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM