[英]What is the correct way to make a NSWindowController Singleton in Swift?
I have a sample project as: 我有一个示例项目:
https://github.com/ericgorr/nspanel_show.git https://github.com/ericgorr/nspanel_show.git
My project is a storyboard, document based application. 我的项目是一个基于文档的故事板应用程序。 I would like to use a custom segue to toggle the visible state of the inspector window. 我想使用自定义segue来切换检查器窗口的可见状态。 What I have should work, but I cannot quite determine how to make the inspector window a singleton. 我有什么应该工作,但我不能确定如何使检查员窗口成为单身人士。
I believe I should start with: 我相信我应该从:
class InspectorWindowController: NSWindowController
{
static let sharedInstance = InspectorWindowController()
// override func init()
// {
//
// }
override func windowDidLoad()
{
super.windowDidLoad()
NSLog( ":::: %@", InspectorWindowController.sharedInstance );
}
}
But exactly what the initialization should look like in my situation is escaping me, especially since the window is inside of a storyboard. 但是,在我的情况下,初始化应该是什么样子才能逃避我,特别是因为窗口在故事板内部。
Here's how I would modify your code: 以下是我修改代码的方法:
Main.storyboard
give your InspectorWindowController an identifier, such as "Inspector Window Controller" 在Main.storyboard
为InspectorWindowController提供一个标识符,例如“Inspector Window Controller” In InspectorWindowController
, implement your singleton as follows: 在InspectorWindowController
,按如下方式实现单例:
static let shared: InspectorWindowController = { let storyboard = NSStoryboard(name:"Main", bundle: nil) let controller = storyboard.instantiateController(withIdentifier: "Inspector Window Controller") return controller as! InspectorWindowController }()
In Main.storyboard
delete the segue from WindowController
to InspectorWindowController
在Main.storyboard
删除从WindowController
到InspectorWindowController
的segue
In WindowController
replace the showMyPanel()
and hideMyPanel()
IBActions with: 在WindowController
将showMyPanel()
和hideMyPanel()
IBActions替换为:
@IBAction func toggleInspectorPanel( _ sender: AnyObject ) { let inspectorWindow = InspectorWindowController.shared.window! if inspectorWindow.isVisible { inspectorWindow.orderOut(self) } else { inspectorWindow.makeKeyAndOrderFront(self) } }
Also in WindowController
, remove the NSLog()
call from windowDidLoad()
. 同样在WindowController
,从windowDidLoad()
删除NSLog()
调用。 It causes a recursive call to the InspectorWindowController.shared
initialization code. 它会导致对InspectorWindowController.shared
初始化代码的递归调用。
Main.storyboard
link the Inspector toolbar button to toggleInspectorPanel()
在Main.storyboard
将Inspector工具栏按钮链接到toggleInspectorPanel()
The InspectorWindowController.shared
singleton will be initialized, and the inspector panel loaded (but not shown), the first time it is referenced. 将初始化InspectorWindowController.shared
单例,并在第一次引用检查器面板时加载(但未显示)。
You can select the window controller from the window controller scene and in the attributes inspector select Single
from the pop up under Presentation
. 您可以从窗口控制器场景中选择窗口控制器,并在属性检查器中从“ Presentation
下的弹出窗口中选择“ Single
”。 This will ensure the show segue only uses a single instance of the window controller. 这将确保show segue仅使用窗口控制器的单个实例。 See this answer for more information. 有关更多信息,请参阅此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.