从NSViewController在OS X上加载nib

时间:2015-09-11 06:28:40

标签: macos nswindow nib

我尝试在OS X上从NSWindow加载NSViewController,并且我正在执行以下操作:

private lazy var discoverable: DiscoverableWindow = {
     return DiscoverableWindow.instanceFromNib()
} ()

静态方法instanceFromNib()定义如下:

class func instanceFromNib() -> DiscoverableWindow {
    var instance = DiscoverableWindow()
    var objects: NSArray?
    NSBundle.mainBundle().loadNibNamed("DiscoverableWindow", owner: instance, topLevelObjects: &objects)
    return instance
}

我正在使用该窗口显示NSViewController

    NSApp.beginSheet(self.discoverable, modalForWindow: NSApplication.sharedApplication().mainWindow!, modalDelegate: nil, didEndSelector: nil, contextInfo: nil)

但是,当我加载它时,我看到以下内容:

http://192.168.3.200:3000

我有什么不正确的事吗?为什么NSWindow为空?我在下面阅读了以下内容:

enter image description here

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

Cocoa - loadNibNamed:owner:topLevelObjects: from loaded bundle

1 个答案:

答案 0 :(得分:1)

为什么不制作一个窗口控制器来处理DiscoverableWindow

创建NSWindowController的子类,确保选中“还为用户界面创建xib文件”。在DiscoverableWindowController xib中配置窗口,在窗口的属性检查器上取消选中“Visible At Launch”。

enter image description here

然后,在ViewController

@IBAction func showSheet(sender: NSButton) {
    let discoverableWC = DiscoverableWindowController(windowNibName: "DiscoverableWindowController")
    view.window?.beginSheet(discoverableWC.window!, completionHandler: nil)
}

通常,应用中的每个窗口都应该由自己的窗口控制器管理,让窗口控制器为您处理nib加载和实例化。

Download the sample project here