AppDelegate的Cocoa应用程序使用故事板在Xcode 6 [英] AppDelegate for Cocoa app using Storyboards in Xcode 6

查看:255
本文介绍了AppDelegate的Cocoa应用程序使用故事板在Xcode 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的OS X应用程式,转换为故事板作为主要介面后,我的应用程式委托不再使用。之前,MainMenu.xib有一个App Delegate对象,我可以将其类设置为我的应用程序委托。但是,故事板不包含此类对象。

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.

如何让我的AppDelegate回来并保留故事板?

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

推荐答案

如果您不指定它是一个基于文档的应用程序, Xcode将创建一个AppDelegate.swift类,并为您连接它在应用程序场景。

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.

截至目前(Xcode Beta-2),新的基于文档的应用程序不要有一个存根AppDelegate.swift文件。相反,有ViewController.swift和Document.swift。更糟糕的是,Document.swift文件不正确地实例化了文档的Main.storyboard。

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.

这是一种工作方式:


  1. 创建AppDelegate类(例如:采用NSApplicationDelegate协议的NSObject)

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

从Application对象中拖拽一个Object对象,将Object对象从Object对象拖放到Main.storyboard的Application Scene中,并将其设置为AppDelegate类。应用程序场景到AppDelegate对象,并连接其代理。

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

从Main.storyboard中删除所有其他内容并为文档创建一个新的Document.storyboard窗口。更改Document.swift文件以实例化Storyboard而不是Main。

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.

如果您希望有一个主应用程序窗口和/您的文档窗口,为这些窗口创建一个Application.storyboard和/或Preferences.storyboard,并使用AppDelegate类来实例化它们。这样,AppDelegate可以自定义主窗口外观,并做其他方便的事情,包括从应用程序中的任何窗口发送的IBActions。

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.

这是一个基于文档的应用程序的AppDelegate.swift文件的工作示例,单独的主应用程序窗口和非模态 首选项窗口:

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) }
    }

}

这篇关于AppDelegate的Cocoa应用程序使用故事板在Xcode 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆