如何在 Objective-C 中初始化窗口?
对于 Mac 图形应用程序(不是 iPhone),我需要类似于 Java 中的 main 方法,这是让程序运行的第一个方法。我一直在研究 NSViewController 和 NSWindow 对象之类的东西。我环顾四周,但在任何地方都找不到这个看似简单问题的答案。 (顺便说一句,我对此很陌生)
谢谢
For a Mac graphics application (not iPhone), I need something like a main method in Java, the first method that gets a program going. I've been looking at things like NSViewController and NSWindow object. I've looked around but can't find an answer to this seemingly easy question anywhere. (I am very new at this by the way)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从 Xcode“Cocoa Application”模板之一创建 Cocoa 项目,您将获得一个 main.m 文件,其中包含常用的启动代码。
尝试完成您将在文档中找到的教程项目之一。
If you create a Cocoa project from one of the Xcode "Cocoa Application" templates, you'll get a main.m file that includes the usual startup code.
Try working through one of the tutorial projects that you'll find in the documentation.
如果您使用项目模板,则启动代码位于 ApplicationDelegate 文件中。对于 Cocoa 应用程序,
main.m
设置运行循环,运行 NSApplication(正如您在“Principle class”键下的 Info.plist 文件中看到的那样)。然后加载 nib 文件在 Info.plist 文件中指定(在“主 nib 文件基本名称”项下),默认值是 MainMenu.xib。现在看一下这个 nib 文件,
该文件已由模板设置为具有一个 。 NSApplication 的“文件所有者”(加载 nib 的类),但还有一个代表应用程序委托的蓝色块,该委托方法已填充了一个委托方法和一个出口。
该委托方法是
applicationDidFinishLaunching: 此方法由应用程序调用。这只是它可以处理的可能的委托方法之一,但它是在运行循环启动之后但在应用程序接收任何事件之前发送的。是初始化代码。在这里,您应该开始设置窗口,您可以使用预先提供的
window
插座来访问该窗口。这只是一个快速总结。一个方便的参考是 Cocoa With Love 和 Apple 应用程序简介 架构文档。
If you are using the project templates, the startup code is in the ApplicationDelegate file. The
main.m
, for a Cocoa application, sets up the run loop, runs NSApplication (as you can see in the Info.plist file under the 'Principle class' key.This then loads the nib file that is specified in the Info.plist file (under the 'Main nib file base name' key). The default is MainMenu.xib. Now have a look at this nib file.
This is already set up by the template to have a 'Files's Owner' of NSApplication (the class that loaded the nib) But There is also a blue block which represents the application delegate. This is already filled out with one delegate method one outlet.
The delegate method is
applicationDidFinishLaunching:
This method is called by the application. This is only one of the possible delegate methods that it can handle, but it is sent after the run loop is started but before the application receives any events. It is the common place to put your initialisation code. It is in here that you should start to set up your window, which you can get to using the pre-suppliedwindow
outlet.This is just a quick summary. A handy referenece is on Cocoa With Love and Apple's Introduction to Application Architecture document.