I have multiple container views on MacOS (Xcode/AppKit) which all embed segue to the same PistonViewController class, as in below image.
Each container view has an identifier, e.g. "multiPiston1, multiPiston2", etc…
I would like the PistonViewController has an array of buttons with IBActions. On button action, I would like to get the identifier of the correct Container View, so that I know in which view the action is taking place.
I have tried the following in the PistonViewController class:
class pistonViewController: NSViewController {
...
@IBAction func pistonClick(_ sender: NSButton) {
if self.view.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1")
if self.parent.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1")
if self.parent.view.identifier == NSUserInterfaceItemIdentifier(rawValue: "multiPiston1")
} }
All of them, upon printing, give the wrong identifiers. So how do I find this identifier?
EDIT: Alternatively, each embed segue is separate – if I could somehow get the identifier of the embed segue, then I could also find out which Container View is being used.
EDIT: (my understanding of) web of relationships.
View Controllers: MainController -> PistonViewController
NSViews: MainControllerView -> ContainerView -> PistonView (view of PistonViewController)
2
Answers
NSViewControllers and NSViews live in separate domains, so to speak. So since the ContainerView is not a ViewController, to solve this problem it was necessary to communicate via NSViews.
Subclass the ContainerView and set identification attributes in the subview, i.e. the NSView of the PistonViewController:
The view subclass of the PistonViewController:
Access the NSView attributes of the PistonViewController through self.view:
You can set Restoration ID for each Container on Storyboard and use like this
}
Set Restoration ID here