I have a window with an NSViewController that contains a ContainerView and a button. The ContainerView embeds a TabViewController, which has 4 child NSViewControllers. Here's what I'm describing:
Window
+- ViewController
+- Button
+- ContainerView
+- TabViewController
+- Tab1 ViewController
+- Tab2 ViewController
+- Tab3 ViewController
+- Tab4 ViewController
Everything is wired up in IB and the tab transitions are working.
When the button in the main VC is pressed, I want to read the values of controls on the displayed tab's VC. So, I need a way to traverse this hierarchy programmatically to get from the main VC down to the TabViewController, figure out which tab is displayed, and then down to that tab's VC to read the IBOutlet properties for the controls.
My question is, how can I do this?
On the main VC, I created an IBOutlet property for the ContainerView:
// in main VC
@IBOutlet weak var containerView: NSView!
And in button's IBAction method, I can iterate over the subviews (of which there is only one):
// in main VC
@IBAction func saveBtn(_ sender: Any) {
for view in containerView.subviews {
print(view.className)
}
}
Where I am stuck is how to go from the subview to the corresponding ViewController - in this case NSTabViewController? I'm also not sure if my understanding is solid because the above code prints "NSView" not "NSTabView" as I expect.
I found one possible solution. You can reference the view controllers through their Storyboard segues: