xcode 应用程序代理 - 可可
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application Delegate - Cocoa
提问by lab12
I want to incorporate an applicationDidFinishLaunching: into my cocoa delegate. How would I do this?? On the iphone SDK the applicationDidFinishLaunching is already in the application delegate, but when making my mac application I noticed that there were none.
我想将 applicationDidFinishLaunching: 合并到我的可可委托中。我该怎么做??在 iphone SDK 上 applicationDidFinishLaunching 已经在应用程序委托中,但是在制作我的 mac 应用程序时我注意到没有。
Best Regards,
此致,
Kevin
凯文
回答by Peter Hosey
As of Xcode 3.2, the Mac application template also comes with an application delegate, already connected, that has such a method.
从 Xcode 3.2 开始,Mac 应用程序模板还带有一个应用程序委托,已经连接,具有这样的方法。
To set this up in a project created before Xcode 3.2, create a new class for your delegate to be an instance of. I usually name mine “AppDelegate”. You'll do this by right-clicking on the Classes group and choosing “Add File”, then picking the Cocoa NSObject Subclass file template.
要在 Xcode 3.2 之前创建的项目中进行设置,请为您的委托创建一个新类作为其实例。我通常将我的名字命名为“AppDelegate”。您将通过右键单击“类”组并选择“添加文件”,然后选择 Cocoa NSObject 子类文件模板来完成此操作。
Open the header you just created (AppDelegate.h). Give it any instance variables you want. Then hit Go to Counterpart. That takes you to the implementation file (AppDelegate.m). Add your applicationDidFinishLaunching:
instance method here. Unlike on the iPhone, this is a notification-handler method, so it takes an NSNotification instance and not an NSApplication instance.
打开您刚刚创建的标头 (AppDelegate.h)。给它任何你想要的实例变量。然后点击 Go to Counterpart。这会将您带到实现文件 (AppDelegate.m)。applicationDidFinishLaunching:
在此处添加您的实例方法。与 iPhone 不同,这是一个通知处理程序方法,因此它需要一个 NSNotification 实例而不是一个 NSApplication 实例。
Now to hook it up. In the Resources group, open MainMenu.nib. Drag an Object from the Library window into the top-level nib window (the one with icons in it, such as File's Owner and First Responder). Select the object you just created and open the Identity inspector. Set the object's class to AppDelegate, matching the name you used in Xcode. Right-click on the File's Owner, and drag from its delegate
outlet to your new object.
现在把它连接起来。在资源组中,打开 MainMenu.nib。将对象从“库”窗口拖到顶层 nib 窗口(其中带有图标的窗口,例如“文件所有者”和“第一响应者”)。选择您刚刚创建的对象并打开身份检查器。将对象的类设置为 AppDelegate,与您在 Xcode 中使用的名称相匹配。右键单击 File's Owner,然后从其delegate
出口拖动到您的新对象。
In Xcode, add an NSLog statement to your applicationDidFinishLaunching:
method. Hit Save All, then Build and Go. Switch back to Xcode and open the Debugger Console. If you did everything right and I didn't forget anything, you should see the log message there.
在 Xcode 中,将 NSLog 语句添加到您的applicationDidFinishLaunching:
方法中。点击 Save All,然后点击 Build and Go。切换回 Xcode 并打开调试器控制台。如果您做对了一切并且我没有忘记任何事情,您应该在那里看到日志消息。
回答by Michael
- (id)init
{
if (self = super init]) {
[NSApp setDelegate:self];
}
return self;
}
You can also do this in Interface Builder; from "File's Owner" in MainMenu.xib, just drag the "delegate" outlet to your object. You may want to consider using -awakeFromNib
instead though.
您也可以在 Interface Builder 中执行此操作;从 MainMenu.xib 中的“文件所有者”,只需将“委托”出口拖到您的对象上。不过,您可能要考虑使用-awakeFromNib
。
回答by Graham Ashton
Were you missing the application delegate files altogether? It seems as though there's a bug in the Xcode installation scripts (at least for 3.2.1 on Snow Leopard) that installs the latest project templates in the wrong folder. The older template for a "Cocoa Application" project doesn't contain the delegate files.
您是否完全遗漏了应用程序委托文件?似乎 Xcode 安装脚本(至少对于 Snow Leopard 上的 3.2.1)中存在一个错误,该错误将最新的项目模板安装在错误的文件夹中。“Cocoa Application”项目的旧模板不包含委托文件。
I've explained what I've discovered (and how I "fixed" it) in a blog post called Fixing the Xcode Project Templates.
我在一篇名为Fixing the Xcode Project Templates的博客文章中解释了我的发现(以及我如何“修复”它)。
Cheers, Graham
干杯,格雷厄姆