Nib的自定义类不会连接到类

时间:2015-06-27 00:50:05

标签: macos swift cocoa interface-builder

我正在加载的笔尖是我的应用程序的自定义关于窗口。当按下'关于'NSMenuItem时,我按照以下方式在AppDelegate中加载nib:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var about = NSWindowController()

@IBAction func aboutClicked(sender: NSMenuItem) {
    about = AboutWindow(windowNibName: "AboutWindow") as AboutWindow
    NSBundle.mainBundle().loadNibNamed("AboutWindow", owner: about, topLevelObjects: nil)
}

*我想要连接到nib和nib本身的类都命名为'AboutWindow'。

一旦我创建了NSWindowController和它的nib文件,nib的NSWindow的自定义类选项不允许我放入我在nib旁边创建的'AboutWindow'类。

enter image description here

正如您所看到的,nib的自定义类设置为NSWindow并且不会更改

非常感谢有关如何连接此自定义类和nib的任何帮助。

1 个答案:

答案 0 :(得分:1)

您能描述一下您不想做的事情:

//
//  AboutWindowController.swift
//  AboutWindow
//

import Cocoa

class AboutWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

}

Xcode自动将其连接到AboutWindowController.xib,其名称我更改为AboutWindow.xib

下一个文件:

//
//  AppDelegate.swift
//  AboutWindow
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    var about = NSWindowController()


    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    @IBAction func aboutClicked(sender: NSMenuItem) {
        about = AboutWindowController(windowNibName: "AboutWindow")
        about.showWindow(self)

    }


}

在IB中,我从About菜单项拖到First Responder对象以挂钩动作。当我运行应用程序并单击“关于”菜单项时,将显示AboutWindow。

我没有尝试更改IB中的任何类名。

对评论的回应:

如果我将AboutWindowController更改为:

//
//  AboutWindowController.swift
//  AboutWindow
//

import Cocoa

class AboutWindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

        println("The About window has loaded")
    }

}

我在控制台中看到了消息。