【问题标题】:How to switch between storyboards without using tab bar controller如何在不使用标签栏控制器的情况下在情节提要之间切换
【发布时间】:2018-09-02 03:44:33
【问题描述】:

我有 3 个故事板(主、第一和第二),故事板主在其左上角的视图内包含两个按钮(按钮:第一,按钮:第二),不使用标签栏控制器如何在主情节提要中的第一和第二,同时保持两个按钮始终可见?我已经尝试使用故事板参考,但是在选择其中一个按钮中,它只是在选定的故事板中,但由于需要从主故事板中的视图容器中看到按钮,因此容器视图似乎是一个选项,但不确定如何在该视图容器中的情节提要之间切换,同时保持按钮可见。

请帮忙。谢谢

【问题讨论】:

    标签: swift xcode tvos


    【解决方案1】:

    您不必在 Interface Builder 中使用 Storyboard Reference。您以编程方式创建引用,无论如何我觉得它更简单、更容易理解。

    界面生成器

    通过选中“是初始视图控制器”,确保将 FirstViewControllerSecondViewController 设置为各自故事板的初始控制器。

    代码

    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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2015-06-12
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多