简体   繁体   English

如何确保可可中一个窗口仅显示一次?

[英]How can I make sure a window is only displayed once in Cocoa?

I have an NSWindow which contains an NSImageView. 我有一个NSWindow包含一个NSImageView。 This window gets activated everytime I click on a cell in my tableview. 每次我单击表视图中的单元格时,都会激活此窗口。 I only want 1 instance of the NSWindow to appear, but want to be able to change the contents of NSImageView. 我只希望出现1个NSWindow实例,但希望能够更改NSImageView的内容。

How can I initialize NSWindow and display only 1 instance of it? 如何初始化NSWindow并仅显示1个实例?

One possibility to do this is to create an NSWindowController subclass and an associated window XIB that gets loaded when the window controller is instantiated. 一种可能的方法是创建一个NSWindowController子类和一个关联的窗口XIB,该窗口XIB在实例化窗口控制器时加载。

I'm sure you already have some controller class handling the mouse click in the NSTableView. 我确定您已经有了一些控制器类来处理NSTableView中的鼠标单击。 In that class, simply keep around an instance of the NSWindowController subclass mentioned above as an instance variable. 在该类中,只需保留上面提到的NSWindowController子类的实例作为实例变量即可。 Whenever you need to display the window, tell that ivar to display its window. 每当需要显示窗口时,告诉ivar显示其窗口。

If the window's contents are dependent on the clicked table cell, simply add some methods to the window controller that modify its window's contents and call these methods in your click-handling method before you display the window. 如果窗口的内容取决于单击的表格单元格,则只需在窗口控制器中添加一些方法即可修改其窗口的内容,并在显示窗口之前在单击处理方法中调用这些方法。

btw: I wouldn't use a singleton here because in this case it would just be a workaround for bad design (just my opinion, not a hard fact). 顺便说一句:我不会在这里使用单例,因为在这种情况下,这只是解决不良设计的一种解决方法(只是我的看法,不是硬道理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM