質問

I have a question regarding enabling the File Open menu item for a OSX Cocoa App.

I have created a openDocument method in the AppDelegate and hooked up the menu item to the method and have verified the method gets called when I click on the file open menu.

 - (IBAction)openDocument:(id)sender

My question is, is this really the way to implement the file open menu functionality? I was half expecting Cocoa to automatically display the open panel dialog instead of me having to write the code in the openDocument method to do it. Is this not the case?

役に立ちましたか?

解決

NSDocumentController has a default implementation of openDocument:.
For non document-based applications, you have to provide a custom implementation (like you did).

The reason probably is, that for document based apps, the document controller can create an instance of NSDocument with the contents of the URL returned by the open panel.
For other apps, it's less obvious what the app should do after the user selected a file. So you have to specify that behaviour via code.

If your app fits the document-based model, you could take a look at the Document-Based App Programming Guide. You get a lot of default behaviour for free when adopting the Cocoa document architecture.

Xcode creates all necessary classes when you check the "Create Document Based Application" checkbox in the "New Project" wizard.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top