What happens when the "create document-based application" option is chosen in Xcode?
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. An开发者_高级运维d 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 ?
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.
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 NSDocument and 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.
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.
精彩评论