在 Xcode 中选择“创建基于文档的应用程序”选项时会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5227759/
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
What happens when the "create document-based application" option is chosen in Xcode?
提问by Stanley
When going through some sample code, notice that setting the main Window Title in IB has no effect when the "create document-based application" option is chosen. The Window would come out with "Untitled" as its title. And we need to override - (NSString *) displayName {} in MyDocument : NSDocument inorder to set the window title. Just wish that some experts in xcode can explain why this is so. That is, what is added to the system when we check the "create document-based application" option when the project is initially generated ?
在查看一些示例代码时,请注意在选择“创建基于文档的应用程序”选项时,在 IB 中设置主窗口标题不起作用。Window 会以“Untitled”作为标题出现。我们需要在 MyDocument : NSDocument 中覆盖 - (NSString *) displayName {} 以设置窗口标题。只希望 xcode 的一些专家可以解释为什么会这样。也就是说,当我们在最初生成项目时选中“创建基于文档的应用程序”选项时,系统会添加什么?
采纳答案by Caleb
Understand that when you create a project from a template in Xcode, Xcode doesn't really generate any code. Sure, it might fill in a few blanks with your name, your project name, etc. But what you're getting is essentially canned starter code for the type of project you've chosen. What's being added when you check the 'document based' option is a NSDocument subclass customized with a reasonable name for your app.
了解当您从 Xcode 中的模板创建项目时,Xcode 并不会真正生成任何代码。当然,它可能会用您的姓名、您的项目名称等填充一些空白。但是您得到的实际上是您选择的项目类型的罐头启动代码。当您选中“基于文档”选项时添加的是一个 NSDocument 子类,它为您的应用程序定制了一个合理的名称。
Take a look at Apples "Human Interface Guidelines", such as this, and you'll see that document windows are supposed to be titled based on the document that they're displaying. For that reason, the default behavior for a document-based application is to set the title to the filename of the document file.
看看苹果的“人机界面指南”,比如这个,你会看到文档窗口应该根据它们显示的文档来命名。因此,基于文档的应用程序的默认行为是将标题设置为文档文件的文件名。
Finally, look at the reference page for NSDocumentand you'll find that the behavior you're seeing is part of the NSDocument class. In particular, if you look at the -displayName method, you'll find information about how to properly customize the window title.
最后,查看NSDocument的参考页面,您会发现您看到的行为是 NSDocument 类的一部分。特别是,如果您查看 -displayName 方法,您将找到有关如何正确自定义窗口标题的信息。
回答by ughoavgfhw
The window controller can freely override the title given to a window in IB. In a non-document based application, the default window controllers will use the window's title because they have no reason not to. However, when a document creates it's window controllers, it tells them that they are connected to it. They then look at the document's displayName property and update the window title in response. The default implementation simply sets the window's title to be the same as the document's, which is untitled until the file has been saved.
窗口控制器可以自由地覆盖给 IB 中的窗口的标题。在非基于文档的应用程序中,默认窗口控制器将使用窗口的标题,因为它们没有理由不这样做。然而,当一个文档创建它的窗口控制器时,它会告诉他们它们已连接到它。然后他们查看文档的 displayName 属性并更新窗口标题作为响应。默认实现只是将窗口的标题设置为与文档的标题相同,在文件被保存之前它是无标题的。