在 Objective-C 中应用程序启动时执行一个类
我有一个用 Objective-C 编写的演示应用程序,它使用 Dave DeLong 的 DDHotKey 类(顺便说一句,这是一段出色的编码),我想知道我应该在哪里查看如何在应用程序启动时立即启动该类?
具体来说,该类中有两个函数,registerhotkey(Dave DeLong 提供的示例代码中的 registerexample1)和 unregisterhotkey(Dave DeLong 提供的示例代码中的 unregisterexample1),我想分别在程序执行和程序关闭时运行。
我不太确定如何做到这一点,并且正在寻找关于我应该在哪里查看的指南或只是一些基本的指导。
谢谢!
I have a demo application written in Objective-C that makes use of Dave DeLong's DDHotKey class (a brilliant piece of coding, btw) and I was wondering where should I look at getting the class to launch as soon as the application begins?
Specifically, there are two functions in the class, registerhotkey (registerexample1 in the example code provided by Dave DeLong) and unregisterhotkey (unregisterexample1 in the example code provided by Dave DeLong) that I would like to run at program execution and at program close respectively.
I'm not really sure how to do this and am looking either for a guide as to where I should look or just some basic pointers.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用应用委托中的
applicationDidFinishLaunching:
方法。这在启动时被调用。当应用程序即将退出时,将调用applicationWillTerminate:
方法。或者,您可以将调用放在主函数中,在调用 NSApplicationMain 之前调用
registerhotkey
,在调用 NSApplicationMain 之后调用unregisterhotkey
。如果还没有,您将需要在此代码周围添加一个自动释放池。最后,您可以使用特殊的
load
方法在加载类或类别时调用registerhotkey
。实际上,您不需要调用unregisterhotkey
因为系统会在您的应用程序退出时自动执行此操作。The easiest way to do it is in the
applicationDidFinishLaunching:
method in your app delegate. This is called on startup. TheapplicationWillTerminate:
method will be called when the application is about to exit.Alternatively, you could place the calls in your main function, calling
registerhotkey
before the call to NSApplicationMain, andunregisterhotkey
after the call to NSApplicationMain. If there isn't one already, you will need to add an autorelease pool around this code.Finally, you could use the special
load
method to callregisterhotkey
when a class or category is loaded. You actually don't need to callunregisterhotkey
because the system will do it automatically when your application quits.