我有一个现有的OS X应用程序,在转换为Storyboards作为主界面后,我的app代理不再被使用.之前,MainMenu.xib有一个"App Delegate"对象,我可以将它的类设置为我的app delegate.但是,Storyboard不包含此类对象.
如何恢复AppDelegate并保留故事板?我觉得我错过了一些明显的东西.
如果您没有将它指定为基于文档的应用程序,Xcode将创建一个AppDelegate.swift类并在应用程序场景中为您连接它.
截至目前(Xcode Beta-2),新的基于文档的应用程序没有附带存根AppDelegate.swift文件.相反,有ViewController.swift和Document.swift.更糟糕的是,Document.swift文件错误地为文档实例化了相同的Main.storyboard.
这是我开始工作的一种方式:
创建一个AppDelegate类(例如:采用NSApplicationDelegate协议的NSObject)
将Object对象从Object库拖到Main.storyboard的Application Scene中,并将其设置为AppDelegate类.
按住Control键从Application Scene中的Application对象拖动到AppDelegate对象,并连接其委托.
从Main.storyboard中删除其他所有内容,并为Document窗口创建一个新的Document.storyboard.更改Document.swift文件以实例化该Storyboard而不是Main.
如果除了文档窗口之外还想要一个主应用程序窗口和/或首选项窗口,请为这些窗口创建Application.storyboard和/或Preferences.storyboard,并使用AppDelegate类来实例化它们.这样,AppDelegate可以自定义主窗口外观并执行其他方便的操作,包括接收从应用程序中的任何窗口发送的IBActions.
这是一个基于文档的应用程序的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) }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10011 次 |
| 最近记录: |