Storyboard 在swift5中按下特定按钮时,如何使用代码显示tabBarViewController(在故事板中创建)?
我有一个名为viewController的viewController,上面有一个按钮,我想在按下按钮时显示一个tabBarController(它已经包含两个viewController),我想全屏显示我已经尝试了一个代码Storyboard 在swift5中按下特定按钮时,如何使用代码显示tabBarViewController(在故事板中创建)?,storyboard,uitabbarcontroller,ios13,swift5,xcode11,Storyboard,Uitabbarcontroller,Ios13,Swift5,Xcode11,我有一个名为viewController的viewController,上面有一个按钮,我想在按下按钮时显示一个tabBarController(它已经包含两个viewController),我想全屏显示我已经尝试了一个代码 let tabbar = (self.storyboard?.instantiateViewController(identifier: "TabBar") as? UITabBarController) tabbar!.
let tabbar = (self.storyboard?.instantiateViewController(identifier: "TabBar") as? UITabBarController)
tabbar!.modalPresentationStyle = .fullScreen
self.present(tabbar!, animated: true)
标识符实际上是我给故事板上tabBar控制器的故事板id,
此代码实际上可以工作,但当我按下触发该代码的按钮时,它可以正常工作,但当我在下一次构建时连接该选项卡栏控制器上的ViewController(该选项卡栏上的ViewController)的出口时似乎变黑了,我看不到我在故事板上设计的任何组件,我在控制台中也收到了这条消息
尝试在其视图不在窗口层次结构中的(从)上显示。
请给我一个解决方案和一点解释
您可以尝试将一段从ViewController切换到TabController 现在输入一个标识符 在第一个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)
}