我有一个应用程序,我想要单独的窗口。在启动时加载它们的最佳方法是什么?我想我想让主窗口加载其他窗口。这就是我现在所得到的,不起作用..
我将主窗口控制器子类化,并尝试加载另一个故事板窗口。 (如果需要,我可以在主要的故事板中保留它。)
class MainWindow: NSWindowController {
override init() {
super.init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
let sb = NSStoryboard(name: "SecondStoryboard", bundle: nil)
let win = sb?.instantiateControllerWithIdentifier("WindowTwo") as NSWindowController
win.showWindow(nil)
}
}
最后,我需要能够在控制器之间传递数据。
答案 0 :(得分:1)
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var preferencesPanel: NSPanel!
@IBOutlet weak var transparentCheck: NSButton!
var oldColor:NSColor?
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
oldColor = window.backgroundColor
window.level = screenSaverLevel
preferencesPanel.level = maximumWindowLevelKey
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
@IBAction func transparentWindowAction(sender: AnyObject) {
window.opaque = transparentCheck.state == NSOffState
window.backgroundColor = transparentCheck.state == NSOffState ? oldColor : NSColor(calibratedHue: 0, saturation: 0, brightness: 0, alpha: 0.7)
}
}
答案 1 :(得分:0)
为您需要从头开始加载的所有内容创建ID。使用他们的ID加载您的控制器。
此代码适合我。它基于这篇文章:How does OS X load a storyboard based app, and how does it do window management?
lazy var preferenceWindowController: TYPEPreferenceWindowController? =
{
let theStoryboard :NSStoryboard? = NSStoryboard(name: "Main", bundle: nil)
var thePreferenceWindowController = theStoryboard?.instantiateControllerWithIdentifier("PreferenceWindowController") as TYPEPreferenceWindowController?
return thePreferenceWindowController
}()
@IBAction func showPreferencePanel(sender: AnyObject) {
println("showPreferencePanel")
preferenceWindowController?.showWindow(self)
}