如何在ViewController内部的contavewiew中获取nstabviewController的引用?

发布于 2025-02-12 11:30:21 字数 1073 浏览 0 评论 0原文

我有一个带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2025-02-19 11:30:21

我找到了一个可能的解决方案。您可以通过他们的故事板网络引用视图控制器:

// in main viewcontroller
private weak var generalTabVC : GeneralPrefsViewController?

// in main viewcontroller
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let tabVC = segue.destinationController as? PreferencesTabViewController {
        if let generalTabVC = tabVC.tabViewItems[0].viewController as? GeneralPrefsViewController {
            self.generalTabVC = generalVC
        }
    }
}

// in main viewcontroller
func somefunc() {
    if let generalVC = self.generalVC {
        if generalVC.textField != nil {
            generalVC.textField.stringValue = "hello, world!"
        }
    }
}

I found one possible solution. You can reference the view controllers through their Storyboard segues:

// in main viewcontroller
private weak var generalTabVC : GeneralPrefsViewController?

// in main viewcontroller
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let tabVC = segue.destinationController as? PreferencesTabViewController {
        if let generalTabVC = tabVC.tabViewItems[0].viewController as? GeneralPrefsViewController {
            self.generalTabVC = generalVC
        }
    }
}

// in main viewcontroller
func somefunc() {
    if let generalVC = self.generalVC {
        if generalVC.textField != nil {
            generalVC.textField.stringValue = "hello, world!"
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文