在swift5中按下特定按钮时,如何使用代码呈现tabBarViewController在情节提要中创建?

问题描述

我有一个名为ViewController的viewController,上面有一个按钮,我想在按下按钮时显示一个tabBarController(已经包含两个viewControllers),并且希望它全屏显示,我已经尝试了一个代码

     let tabbar = (self.storyboard?.instantiateViewController(identifier: "TabBar") as? UITabBarController)       
    tabbar!.modalPresentationStyle = .fullScreen
    self.present(tabbar!,animated: true)

标识符实际上是我给情节提要上的tabBar控制器提供的情节提要ID, 该代码实际上有效,但是当我按下触发该代码的按钮时,它工作正常,但是当我连接下一个版本的该tabBar控制器上存在的viewControllers的插座时,那些Viewcontrollers(位于taBbar上的那些)似乎变黑了,我看不到我在情节提要上设计的任何组件,而且我还在控制台中收到此消息

尝试在(来自)上呈现其视图不在窗口层次结构中的。 请给我一个解决方案,并给我一些解释 here is the snapshot of my story board

解决方法

您可以尝试从ViewController到TabController

Set the segue connection

现在放置一个标识符。

Set the segue ID

在第一个ViewController中添加一个func,并使用segue标识符移动到另一个视图。

func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    if segue.identifier == "yourSegueID" {
        let tabBarController = segue.destination as! UITabBarController
        tabBarController.title = "TabBarController"
    }
}

然后添加按钮操作以调用此行

@IBAction func infoMessage(_ sender: UIButton) {
    performSegue(withIdentifier: "yourSegueID",sender: nil)
}

This is the implementation