如何使用Cocoa在Swift中的macOS中打开另一个窗口 [英] How do I open another window in macOS in Swift with Cocoa

查看:424
本文介绍了如何使用Cocoa在Swift中的macOS中打开另一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用macOS应用程序,该应用程序在表视图中显示客户主记录列表.双击表视图中的一行应打开一个新窗口,用户可以在其中查看和编辑客户信息.

I am working on a macOS app that presents a list of customer master records in a table view. Double-clicking a row in the table view should open a new window where the user can view and edit the customer information.

这是一个使用情节提要和Swift的Xcode 8.3.3项目.
它不是文档或核心数据应用程序.

This is an Xcode 8.3.3 project using a storyboard and Swift.
It is not a document or core data app.

我让主窗口工作到表视图正确显示记录的状态,并且关联的视图控制器正在接收双击事件并将其记录到控制台.

I have the main window working up to the point where the table view is displaying the records correctly and the associated view controller is receiving the double-click events and logging them to the console.

我创建了一个附加的窗口控制器并查看编辑窗口,并通过将其临时标记为初始控制器来验证其基本功能.

I have created an additional window controller and view for the edit window and verified its basic functionality by temporarily marking it as the initial controller.

我无法弄清楚的是,当用户双击一行时,如何显示该窗口的新实例.

What I haven't been able to figure out is how to display a new instance of that window when the user double-clicks a row.

感谢@Joshua Nozzi,我离我更近了.这是此时的代码.

Thanks to @Joshua Nozzi I'm closer. Here is the code at this point.

let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let windowController = storyboard.instantiateController(withIdentifier: "xyzzy") as? NSWindowController
{
  windowController.showWindow(self)
}

正在生成

(Storyboard:0x620000000680)不包含带有 标识符"xyzzy"

(Storyboard: 0x620000000680) doesn't contain a controller with identifier 'xyzzy'

错误.

推荐答案

窗口编程指南是了解一般如何管理窗口的好地方.

The Window Programming Guide is a great place to understand how windows are managed in general.

具体来说(假设您知道如何在情节提要中呈现窗口控制器场景),则需要在某个地方存储对新窗口控制器的引用,以便在呈现时不会立即将其释放(并消失).

Specifically (assuming you know how to present a window controller scene in storyboards), you need somewhere to store references to the new window controllers so they’re not immediately deallocated (and disappear) when presented.

在您的情况下,您可能希望将多个打开的详细信息窗口保留在主窗口控制器中,这样,如果主文件消失了,详细信息也将保留.打开详细信息窗口(创建控制器实例并显示其窗口)后,您会将其控制器存储在阵列中.关闭时,您可以从阵列中删除其控制器,以便将其释放.

In your case, you may want to keep an array of your open detail windows in the master window controller, so that if the master goes away, the details do as well. When a detail window is open (a controller instance is created and its window shown), you’ll store its controller in the array; when closed, you remove its controller from the array so it’s deallocated.

有多种方法可以执行此操作,具体取决于您需要多少控件,希望子窗口所有权如何工作等,但是这种基本模式通常就足够了.

There are a number of ways to do this, depending on how much control you want, how you want child window ownership to work, etc., but this basic pattern is usually sufficient.

要从情节提要中实例化新的窗口控制器场景:

To instantiate a new window controller scene from a storyboard:

var myWindowController = NSStoryboard(name: "MyStoryboardFileName", bundle: nil)?.instantiateControllerWithIdentifier("MyWindowControllerIdentifier") as MyWindowControllerClass
myWindowController?.showWindow(self)

这篇关于如何使用Cocoa在Swift中的macOS中打开另一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆