使用Storyboard在AppDelegate中参考主NSWindow? [英] Reference main NSWindow in AppDelegate using Storyboard?
问题描述
我试图在我的应用程序代理中为我的应用程序的主窗口设置一个插座,然后在Interface Builder中连接它。我在我的应用程序代理轻松地创建插座:
@IBOutlet weak var mainWindow:NSWindow!
然而,在界面生成器中没有办法让我将引用插座连接到App Delegate。相反,我只能连接到窗口控制器,我希望这张图片显示:
第一个对象是Window控制器,第二个对象是First Responder,不见了。菜单栏中有App Delegate对象:
我可以将任何东西从菜单栏连接到App Delegate中的任何插座。
图我可以通过使用以下访问窗口对象:
NSApp.windows [0]
但这似乎容易出错,特别是如果我有多个窗口。
解决方案我不知道这是否正确的做法,但这将解决你的问题。
Decalre AppDelegate中的NSWindow属性
weak var window:NSWindow !
并将属性设置为 windowWillLoad NSWindowController
(NSApplication.sharedApplication()。delegate as!AppDelegate).window = self.window
您必须将NSWindowController子类化为定义windowWillLoad
I am attempting to set an outlet for the main window of my app in my App Delegate, then connect it within Interface Builder. I create the outlet within my App Delegate easily:
@IBOutlet weak var mainWindow: NSWindow!
However there's no way, within interface builder, for me to connect a referencing outlet to the App Delegate. Instead, I can only connect it to the Window Controller, which I hope this picture shows:
The first object is the Window Controller and the second object is the First Responder, however the App Delegate object is missing. The menubar has the App Delegate object:
And I can connect anything from the menubar to any outlets in the App Delegate.
I figure I can access the window object by using:
NSApp.windows[0]
But that seems prone to error, especially if I have more than one window.
解决方案I dont know if this is correct way of doing, but this will solve your problem.
Decalre a NSWindow property in AppDelegate
weak var window: NSWindow!
and set the property in something like windowWillLoad of the NSWindowController
(NSApplication.sharedApplication().delegate as! AppDelegate).window = self.window
You will have to subclass NSWindowController to define windowWillLoad
这篇关于使用Storyboard在AppDelegate中参考主NSWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!