macOS,Swift 3:segue之后如何取回数据? [英] macOS, Swift 3: How to get data back after segue?
问题描述
例如:我有两个UIViewController
.第一个具有按钮和NSTextField
,第二个仅具有NSTextField
.
For example: I have two UIViewController
s. The first has a button and a NSTextField
, and the second has a NSTextField
only.
当我单击第一个控制器上的按钮时-第二个控制器显示为弹出窗口.
When I click the button on the first controller — the second controller shows as popover window.
将一些数据从第一个控制器传输到第二个控制器并不重要-我使用segue.但是,我应该怎么做才能将数据从弹出窗口转移回主窗口?
例如::我们打开弹出窗口,在NSTextField
中键入一些文本,我想在第一个ViewController
的NSTextField
中获得此文本.
Making a transfer of some data from first controller to second is not big deal — I use a segue. But what I should to do to transfer data back — from the popover window to main window?
For example: we open the popover window, type some text in NSTextField
, and I want to get this text in the NSTextField
of the first ViewController
.
我找不到答案:-(
推荐答案
在macOS中,如果使用情节提要和segues,则非常简单.
In macOS it's pretty easy if you are using storyboard and segues.
NSViewController
具有属性presenting
,其中包含对父视图控制器的引用.
NSViewController
has a property presenting
which contains a reference to the parent view controller.
获取引用,在父视图控制器中调用方法以传递数据并关闭子视图控制器.
Obtain the reference, call a method in the parent view controller to pass the data and dismiss the child view controller.
let firstViewController = presenting as! FirstViewController
firstViewController.passDataBack(data)
self.dismiss(self)
这篇关于macOS,Swift 3:segue之后如何取回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!