Swift:在Container View / NSView中切换NSViewController

问题描述 投票:6回答:2

我想实现一个非常简单的任务 - 通过按下按钮来更改容器视图的ViewController

enter image description here

在我的示例中,使用Interface Builder将ViewController1嵌入到Container视图中。通过按下按钮ViewController2我想将视图更改为第二个ViewController。

我很困惑,因为如果我创建一个Outlet,容器视图本身似乎是一个NSView,据我所知,NSView不能包含VC。真的很感谢你的帮助!

swift storyboard segue nsview nsviewcontroller
2个回答
8
投票

请注意,要使其工作,您必须向视图控制器添加故事板标识符,这可以转到故事板,然后在右侧窗格中选择Identity Inspector,然后在Storyboard ID子类别中输入Identity

然后这个ViewController的实现将实现你正在寻找的。

import Cocoa

class ViewController: NSViewController {

    // link to the NSView Container
    @IBOutlet weak var container : NSView!

    var vc1 : ViewController1!
    var vc2 : ViewController2!

    var vc1Active : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Make sure to set your storyboard identiefiers on ViewController1 and ViewController2
        vc1 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController1") as! ViewController1
        vc2 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController2") as! ViewController2

        self.addChildViewController(vc1)
        self.addChildViewController(vc2)
        vc1.view.frame = self.container.bounds
        self.container.addSubview(vc1.view)
        vc1Active = true

    }

    // You can link this action to both buttons
    @IBAction func switchViews(sender: NSButton) {

        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }

        if vc1Active == true {

            vc1Active = false
            vc2.view.frame = self.container.bounds
            self.container.addSubview(vc2.view)

        } else {

            vc1Active = true
            vc1.view.frame = self.container.bounds
            self.container.addSubview(vc1.view)
        }

    }
}

3
投票

也许这是一个迟到的答案,但无论如何我会发布我的解决方案。希望它可以帮助某人。

我在ContainerView中嵌入了NSTabViewController。然后,为了不在顶部看到标签我做了这个:

  • 转到故事板中的NSTabViewController
  • 在“属性”检查器中,将样式更改为“未指定”
  • 然后单击Tab Bar View Controller中的TabView,并将样式设置为“tabless”see image here

在此之后你需要:

  • 存储tabViewController对mainViewController的引用,以便从代码中选择选项卡
  • 在mainViewController(您的容器所在的位置)添加一个按钮,您将在其中更改tabViewController中的选项卡。

您可以通过在重写准备segue函数时存储对tabViewController的引用来完成此操作。这是我的代码:

首先将属性添加到mainViewController

private weak var tabViewController: NSTabViewController?

然后重写此函数并保持对tabViewController的引用:

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
            guard let tabViewController = segue.destinationController
                as? NSTabViewController else { return }

            **self.tabViewController = tabViewController as? NSTabViewController**

        }

在此之后,您将参考tabViewController的所有设置。接下来(最后)您需要做的是为按钮执行操作以移动到第一个(或第二个)视图控制器,如下所示:

@IBAction func changeToSecondTab(_ sender: Any) {
        self.tabViewController?.selectedTabViewItemIndex = 0 // or 1 for second VC 
    } 

祝一切顺利!

© www.soinside.com 2019 - 2024. All rights reserved.