3

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 :-(

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72
  • 1
    http://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers/2520/using-the-delegate-pattern-passing-data-back#t=201611080745298224365 (it's in the iOS tag, but it works on macOS, too) – FelixSFD Nov 08 '16 at 07:46
  • You can use an unwindSegue method if your view controllers are in a navigation controller, or you can use a protocol. – user3353890 Nov 08 '16 at 08:46

1 Answers1

5

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)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks! Other guy told me that the "right" way is to use delegates. That video is pretty good for understanding: https://www.youtube.com/watch?v=KhqXUi1SF4s – Max Vecheslavov Nov 08 '16 at 11:39
  • 2
    Youtube hasn't got a seal of quality. Anybody can upload stuff ;-) – vadian Nov 08 '16 at 11:45