【问题标题】:Reference main NSWindow in AppDelegate using Storyboard?使用 Storyboard 在 AppDelegate 中引用主 NSWindow?
【发布时间】:2015-08-21 19:03:05
【问题描述】:

我试图在我的 App Delegate 中为我的应用程序的主窗口设置一个插座,然后在 Interface Builder 中连接它。我很容易在我的 App Delegate 中创建插座:

@IBOutlet weak var mainWindow: NSWindow!

但是,在界面构建器中,我无法将引用插座连接到 App Delegate。相反,我只能将它连接到窗口控制器,我希望这张照片显示:

第一个对象是 Window Controller,第二个对象是 First Responder,但是缺少 App Delegate 对象。菜单栏有 App Delegate 对象:

我可以将菜单栏中的任何内容连接到 App Delegate 中的任何插座。

我想我可以通过以下方式访问窗口对象:

NSApp.windows[0]

但这似乎容易出错,特别是如果我有多个窗口。

【问题讨论】:

  • 为什么要在 App Delegate 中引用 NSWindow ?
  • @Kaunteya 我正在尝试实现applicationShouldHandleReopen:hasVisibleWindows: 并在单击停靠图标时重新打开主窗口,但我无法从 App Delegate 引用主窗口。这是不正确的吗?

标签: xcode swift cocoa interface-builder


【解决方案1】:

我不知道这是否是正确的做法,但这会解决你的问题。

在 AppDelegate 中声明一个 NSWindow 属性

weak var window: NSWindow!

并将属性设置为类似 NSWindowController 的windowWillLoad

(NSApplication.sharedApplication().delegate as! AppDelegate).window = self.window

你必须继承 NSWindowController 来定义 windowWillLoad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2015-08-13
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    相关资源
    最近更新 更多