I'm making a simple OS X app that looks like that in Interface Builder:
Backstory
The MainView
is a subclass of NSViewController
, and the toolbar is a simple NSToolbar
I built with the Interface Builder. By default, the NSToolbar items are enabled, but they have no purpose until Main Image View
contains a NSImage
, so I want my NSViewController
(MainView
) to enable the Toolbars button when it receives an image, and disable them when the "Clear" button is pressed.
The issue
To implement the toolbar actions, I followed this answer, so now I have a MainView.swift
implementing the NSViewController
with the toolbar IBActions in it (and it works), so my toolbar can send actions to my view. But I have no way for the toolbar to receive messages from the NSViewController
, because they are not in the same object: my toolbar is in the Window
, my MainImageView
is in MainView
's View.
Question
From what my search brought up, I should use a delegate, so I started making a simple protocol:
protocol MainToolbarDelegate {
func setButtonsEnabled(to:Bool)
}
However, when setting the var delegate: MainToolbarDelegate
in my class MainView: NSViewController
, I have the same issue: I cannot point to the toolbar because it is not in the same object, and there is no segue linking one to another. I tried var delegate: MainToolbarDelegate = self.view.window.toolbar
but Xcode returns me an error.
So now, my question is: what am I missing? That's the first time I use a delegate so I might be doing something completely wrong. Do I need to subclass my Window so that I could use segues?
Sorry again if my questions are not accurate, I'm completely lost.