AppDelegate for Cocoa app using Storyboards in Xco

2020-02-20 07:16发布

I have an existing OS X app, and after converting to Storyboards as the main interface, my app delegate is no longer being used. Before, the MainMenu.xib had an "App Delegate" object, and I could set its class to my app delegate. However, the Storyboard contains no such object.

How do I get my AppDelegate back and keep storyboards? I feel like I'm missing something obvious.

标签: cocoa xcode6
2条回答
仙女界的扛把子
2楼-- · 2020-02-20 07:25

Here's another cheap and easy way to do it, if all you want to do is customize the appearance of the main window before it appears:

Make your own subclass of NSWindowController, and connect it up as the delegate of the main window.

Implement windowDidUpdate as a hook to the window so you can set up the desired options, but also remove the window delegate so the function only gets called once. This is all the code you need to make that work:

//  WindowController.swift

import Cocoa

class WindowController: NSWindowController, NSWindowDelegate {

    func windowDidUpdate(notification: NSNotification!) {
            if let window = notification.object as? NSWindow! {
                window.titlebarAppearsTransparent = true
                window.titleVisibility = NSWindowTitleVisibility.Hidden
                window.styleMask |= NSFullSizeContentViewWindowMask
                window.delegate = nil  }
    }
}

Actually, an even easier way to apply those appearance options to the window, is by using Interface Builder to add them as User Defined Runtime Attributes to the NSWindow object. You don't need to subclass NSWindowController or write any code at all. Just plug in these values to the window object via the Identity Inspector pane:

Keypath: titlebarAppearsTransparent, Type: Boolean, Value: Checked
Keypath: titleVisibility, Type: Number, Value: 1
Keypath: styleMask, Type: Number, Value: 32783

Of course, you can't specify individual bits of the styleMask, but it's easy enough to add them all together and get a single number to specify the style.

With Storyboard architecture, and the new powers given to NSViewController, there's not as much need to subclass NSWindowController anymore.

查看更多
叛逆
3楼-- · 2020-02-20 07:44

If you don't specify it to be a Document-Based Application, Xcode will create an AppDelegate.swift class and connect it up in the Application Scene for you.

As of right now (Xcode Beta-2), new Document-Based apps don't come with a stub AppDelegate.swift file. Instead, there's ViewController.swift and Document.swift. Worse, the Document.swift file incorrectly instantiates the same Main.storyboard for documents.

Here's one way I got it to work:

  1. Create an AppDelegate class (e.g.: an NSObject that adopts the NSApplicationDelegate protocol)

  2. Drag an Object object from the Object library, into the Application Scene of Main.storyboard and set it to the AppDelegate class.

  3. Control-drag from the Application object in the Application Scene to the AppDelegate object, and connect up its delegate.

  4. Remove everything else from the Main.storyboard and create a new Document.storyboard for the Document window. Change the Document.swift file to instantiate that Storyboard instead of Main.

  5. If you want to have a main application window and/or a preferences window in addition to your document windows, create an Application.storyboard and/or Preferences.storyboard for those windows, and use the AppDelegate class to instantiate them. This way, the AppDelegate can customize the main window appearance and do other handy things, including receiving IBActions sent from any window in the app.

Here's a working example of an AppDelegate.swift file for a Document-Based app that also has a separate, single main Application window, and a non-modal Preference window:

//  AppDelegate.swift

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

    //init() {   
    //    super.init() 
    // remove this if you don't use it
    //}

    var application: NSApplication? = nil
    func applicationDidFinishLaunching(notification: NSNotification) {
        application = notification.object as? NSApplication

        let path = NSBundle.mainBundle().pathForResource("Defaults", ofType: "plist")
        let defaults = NSDictionary(contentsOfFile:path)
        NSUserDefaults.standardUserDefaults().registerDefaults(defaults)
        NSUserDefaultsController.sharedUserDefaultsController().initialValues = defaults
        NSUserDefaultsController.sharedUserDefaultsController().appliesImmediately = true

    }

    func applicationDidBecomeActive(notification: NSNotification) {
        if application?.orderedDocuments?.count < 1 { showApplication(self) }
    }

    //func applicationWillFinishLaunching(notification: NSNotification) {
        // remove this if you don't use it
     //}

    func applicationWillTerminate(notification: NSNotification) {
       NSUserDefaults.standardUserDefaults().synchronize()

    }

    func applicationShouldOpenUntitledFile(app: NSApplication) -> Bool { return false }

    func applicationShouldTerminateAfterLastWindowClosed(app: NSApplication) -> Bool { return false }

    var applicationController: NSWindowController?
    @IBAction func showApplication(sender : AnyObject) {
        if !applicationController {

            let storyboard = NSStoryboard(name: "Application", bundle: nil)
            applicationController = storyboard.instantiateInitialController() as? NSWindowController
            if let window = applicationController?.window {
                window.titlebarAppearsTransparent = true
                window.titleVisibility = NSWindowTitleVisibility.Hidden
                window.styleMask |= NSFullSizeContentViewWindowMask
            }


        }
        if applicationController { applicationController!.showWindow(sender) }
    }

    var preferencesController: NSWindowController?
    @IBAction func showPreferences(sender : AnyObject) {
        if !preferencesController {
            let storyboard = NSStoryboard(name: "Preferences", bundle: nil)
            preferencesController = storyboard.instantiateInitialController() as? NSWindowController
        }
        if preferencesController { preferencesController!.showWindow(sender) }
    }

}

查看更多
登录 后发表回答