Search code examples
swiftmacosswift3seguetransfer

macOS, Swift 3: How to get data back after segue?


For example: I have two UIViewControllers. 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.

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.

I can't find the answer :-(


Solution

  • In macOS it's pretty easy if you are using storyboard and segues.

    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)