我有一个现有的 OS X 应用程序,在转换为 Storyboards 作为主界面后,我的应用程序委托不再被使用。之前,MainMenu.xib 有一个“App Delegate”对象,我可以将其类设置为我的应用程序委托。然而,故事板不包含这样的对象。
如何取回我的 AppDelegate 并保留故事板?我觉得我错过了一些明显的东西。
如果您没有将其指定为基于文档的应用程序,Xcode 将创建一个 AppDelegate.swift 类并将其连接到应用程序场景中。
截至目前(Xcode Beta-2),新的基于文档的应用程序不附带存根 AppDelegate.swift 文件。相反,有 ViewController.swift 和 Document.swift。更糟糕的是,Document.swift 文件错误地实例化了文档的同一个 Main.storyboard。
这是我让它发挥作用的一种方法:
创建一个AppDelegate类(例如:采用NSApplicationDelegate协议的NSObject)
从对象库中拖动一个对象对象到 Main.storyboard 的应用程序场景中,并将其设置为 AppDelegate 类。
按住 Control 键并从应用程序场景中的应用程序对象拖动到 AppDelegate 对象,并连接其委托。
从 Main.storyboard 中删除所有其他内容,并为文档窗口创建一个新的 Document.storyboard。更改 Document.swift 文件以实例化 Storyboard 而不是 Main。
如果除了文档窗口之外,您还希望拥有主应用程序窗口和/或首选项窗口,请为这些窗口创建 Application.storyboard 和/或 Preferences.storyboard,并使用 AppDelegate 类来实例化它们。这样,AppDelegate 可以自定义主窗口外观并执行其他方便的操作,包括接收从应用程序中的任何窗口发送的 IBAction。
以下是基于文档的应用程序的 AppDelegate.swift 文件的工作示例,该应用程序还具有单独的,单独的主应用程序窗口和非模态的偏好窗口:
// 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) }
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)