Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在不使用选项卡栏控制器的情况下在故事板之间切换_Swift_Xcode_Tvos - Fatal编程技术网

Swift 如何在不使用选项卡栏控制器的情况下在故事板之间切换

Swift 如何在不使用选项卡栏控制器的情况下在故事板之间切换,swift,xcode,tvos,Swift,Xcode,Tvos,我有3个故事板(主、第一和第二),故事板主在左上角的视图中包含两个按钮(按钮:第一和按钮:第二),如果不使用选项卡栏控制器,如何在主故事板的第一和第二之间切换,同时始终保持两个按钮可见?我尝试过使用故事板引用,但当选择其中一个按钮时,它只会进入所选的故事板,这不起作用,因为按钮需要在主故事板的视图容器中可见,容器视图似乎是一个选项,但不确定如何在视图容器中的故事板之间切换,同时保持按钮可见 请帮忙。非常感谢。 您不必在Interface Builder中使用情节提要引用。您可以通过编程方式创建引

我有3个故事板(主、第一和第二),故事板主在左上角的视图中包含两个按钮(按钮:第一和按钮:第二),如果不使用选项卡栏控制器,如何在主故事板的第一和第二之间切换,同时始终保持两个按钮可见?我尝试过使用故事板引用,但当选择其中一个按钮时,它只会进入所选的故事板,这不起作用,因为按钮需要在主故事板的视图容器中可见,容器视图似乎是一个选项,但不确定如何在视图容器中的故事板之间切换,同时保持按钮可见

请帮忙。非常感谢。
您不必在Interface Builder中使用情节提要引用。您可以通过编程方式创建引用,我觉得这更简单,也更容易理解

界面生成器 通过选中“是初始视图控制器”,确保将
FirstViewController
SecondViewController
设置为各自故事板的初始控制器

代码
class MainViewController: UIViewController {
    @IBOutlet weak var contentView: UIView!

    // Initialize the first view controller of storyboard
    let firstViewController: FirstViewController = {
        let storyboard = UIStoryboard(name: "First", bundle: nil)
        return storyboard.instantiateInitialViewController() as! FirstViewController
    }()

    // Initialize the second view controller of storyboard
    let secondViewController: SecondViewController = {
        let storyboard = UIStoryboard(name: "Second", bundle: nil)
        return storyboard.instantiateInitialViewController() as! SecondViewController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        show(childViewController: firstViewController)
    }

    @IBAction func showFirstVC(_ sender: Any) {
        // Don't show the first view controller again if it's already visible
        guard firstViewController.parent != self else { return }
        removeAllChildViewControllers()
        show(childViewController: firstViewController)
    }

    @IBAction func showSecondVC(_ sender: Any) {
        // Don't show the second view controller again if it's already visible
        guard secondViewController.parent != self else { return }
        removeAllChildViewControllers()
        show(childViewController: secondViewController)
    }

    // MARK: - Helper methods
    // Show a view controller in the `contentView`
    func show(childViewController vc: UIViewController) {
        self.addChildViewController(vc)
        self.contentView.addSubview(vc.view)
        vc.didMove(toParentViewController: self)
    }

    // Remove a view controller from the `contentView`
    func remove(childViewController vc: UIViewController) {
        vc.willMove(toParentViewController: nil)
        vc.removeFromParentViewController()
        vc.view.removeFromSuperview()
    }

    // Remove all child view controllers
    func removeAllChildViewControllers() {
        for childVC in self.childViewControllers {
            remove(childViewController: childVC)
        }
    }
}