How to Create a Document-Based OS X App that Launches with “Open…” Dialog

给你一囗甜甜゛ 提交于 2019-12-04 15:54:46

That would be customized behaviour.

In your application controller override applicationShouldOpenUntitledFile: to prevent opening a blank document at startup, then display the file dialog.

This is not hard but not obvious and takes a few steps to get going.

Add a window to your MainMenu.xib Set the Visible at launch to NO in the inspector. Now create an NSObject subclass in your project. You might include AppDelegate in the name because you will want to make it the delegate of your app. In the interface header be sure to declare the protocol right after the word NSObject. While there, add an IBOutlet property an NSWindow. Back to the MainMenu.xib ... Add an NSObject (blue cube) to your xib from the library and set its class to your new app delegate class. Next connect your window to the property in your app delegate class and connect the window's delegate outlet to your app delegate.

Now the menu. Locate the View menu in MainMenu and add one NSMenuItem. Give it a title. "My fancy main window" or whatever. Now connect it to your app delegate with both an IBOutlet (in case you want to fiddle with its enabled state or title later ) And add an IBAction for this menu item as well. Something like showMyFancyWindow:

This menu item will be persistent. In your IBAction method call makeKeyAndOrderFront: with your app delegate's property for your window as the argument.

Extra credit

Add a BOOL property to your app delegate. Something like showsMyFancyWindowAtLaunch

Create a constant NSString as a key above your @implementation line. Add a checkbox button to your window. Bind its value to your BOOL. Add an IBAction method for the checkbox. Inside that [[NSUserDefaults sharedDefaults] setBool: self.showsMyFancyWindowAtLaunch forKey: theConstStringKeyYouCreated]

Then in your applicationDidFinishLaunching: Use the corresponding bool:forKey: method of NSUserDefaults to check whether or not to call showMyFancyWindow: method at launch.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!