How to transfer data from NSWindowController to my NSViewController?

I have an NSToolBar button NSToolBar in my NSWindowController class, which is my main window class:

 class MainWindowController: NSWindowController { @IBOutlet weak var myButton: NSButton! // ... } 

I have a MainViewController class, that is, the contents of the NSViewController main window.

How can I access this button in my NSViewController content? Is there a better way to organize IBOutlets and controllers for easier access?

+10
cocoa swift macos
source share
2 answers

What about the delegate? This example will change the name of your button.

 @objc protocol SomeDelegate { func changeTitle(title: String) } class ViewController: NSViewController { weak var delegate: SomeDelegate? @IBAction func myAction(sender: AnyObject) { delegate?.changeTitle("NewTitle") } } class MainWindowController: NSWindowController, SomeDelegate { @IBOutlet weak var myButton: NSButton! override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller window has been loaded from its nib file. let myVc = window!.contentViewController as! ViewController myVc.delegate = self } func changeTitle(title: String) { myButton.title = title } } 
+8
source share

To access the NSViewController from NSWindowController:

 let viewController:MainViewController = self.window!.contentViewController as! MainViewController 

To access the NSWindowController from the NSViewController:

 let windowController:MainWindowController = self.view.window?.windowController as! MainWindowController 
+9
source share

All Articles