【问题标题】:Nib's custom class won't hook up to classNib 的自定义类不会连接到类
【发布时间】:2015-06-27 00:50:04
【问题描述】:

我正在加载的 nib 是我的应用程序的自定义“关于”窗口。当按下“关于”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”类。

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

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

【问题讨论】:

    标签: macos swift cocoa interface-builder


    【解决方案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 对象以连接动作。当我运行应用程序并单击 About 菜单项时,将显示 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")
        }
    
    }
    

    我看到控制台中打印的消息。

    【讨论】:

    • windowDidLoad() 方法不会被调用。奇怪的是,awakeFromNib() 方法的覆盖确实被调用了。什么可能导致这种行为?
    • @Jonathan,请在答案底部查看我的回复。
    • @Jonathan,如果我不调用showWindow(),那么我就看不到窗口,也看不到控制台中的输出。
    • 非常感谢!我认为调用NSBundle.mainBundle().loadNibNamed("AboutWindow", owner: about, topLevelObjects: nil) 就足够了,但我看到需要调用showWindow。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2013-02-01
    • 2017-11-05
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多