如何在ViewController内部的contavewiew中获取nstabviewController的引用?
我有一个带有NSViewController的窗口,该窗口包含一个contavererview和一个按钮。 ContainSerview嵌入了一个TabViewController,该controller具有4个孩子NSViewControllers。这是我要描述的:
Window
+- ViewController
+- Button
+- ContainerView
+- TabViewController
+- Tab1 ViewController
+- Tab2 ViewController
+- Tab3 ViewController
+- Tab4 ViewController
一切都在IB中连接起来,而Tab Transition正在起作用。
按下主VC中的按钮时,我想阅读显示选项卡的VC上的控件值。因此,我需要一种以编程方式遍历此层次结构的方法,以从主VC到达TabViewController,找出显示哪个选项卡,然后向下到该选项卡的VC来读取控件的IBOUTLET属性。
我的问题是,我该怎么做?
在主VC上,我为ContainSerview创建了一个IBOUTLET属性:
// in main VC
@IBOutlet weak var containerView: NSView!
在按钮的IBACTION方法中,我可以在子视图(只有一个)上迭代(其中一个):
// in main VC
@IBAction func saveBtn(_ sender: Any) {
for view in containerView.subviews {
print(view.className)
}
}
我被粘住的地方是如何从子视图转到相应的View ConvietController - 在这种情况下,nstabviewcontroller?我也不确定我的理解是否牢固,因为上述代码正如我期望的那样打印的“ NSView”而不是“ nstabview”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个可能的解决方案。您可以通过他们的故事板网络引用视图控制器:
I found one possible solution. You can reference the view controllers through their Storyboard segues: