如何在 [=10th=] swift 中以编程方式将故事板 viewcontroller 加载到标签栏控制器中?

How to load storyboard viewcontroller into programmatically tabbar controller in ios siwft?

我有以编程方式完成的标签栏控制器。标签栏工作正常。在 stroyboard 中,我创建了 firstViewController 并分配了 class 名称 firstViewController。当我尝试在选项卡栏控制器上的第一个 vc 上进行制表以导航到第一个 viewcontroller 时,它崩溃了。如果我以编程方式给出意味着它工作正常。

如何以编程方式从选项卡栏控制器导航到故事板视图控制器。

这是我的标签栏控制器代码:

 let tabBarCnt = UITabBarController()
 func createTabBarController() {

    let firstVC = FViewController()
    firstVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)

    let secondVC = SViewController()
    secondVC.tabBarItem = UITabBarItem(tabBarSystemItem: .more, tag: 1)

    let thirdVC = ViewController()
    thirdVC.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 2)

    let fourthVC = ViewController()
    fourthVC.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 3)


    if UIDevice.current.userInterfaceIdiom == .pad {
        let controllerArray = [firstVC, secondVC, thirdVC, fourthVC]
        tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: [=12=])}

        self.view.addSubview(tabBarCnt.view)
    } else {
        let controllerArray = [firstVC, secondVC]
        tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: [=12=])}

        self.view.addSubview(tabBarCnt.view)
    }

}

这是我的 firstViewController 代码的第一个 VC:

import UIKit

class FViewController: UIViewController, UITabBarDelegate {

@IBOutlet weak var firstBtn: UIButton!

@IBOutlet weak var fview: UIView!
override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor =  UIColor.blue
 // self.fview.backgroundColor = .brown


    // Do any additional setup after loading the view.
   }


}

请尝试以下代码。您可以通过界面生成器提供视图控制器标识符。

let tabBarCnt = UITabBarController()
func createTabBarController() {
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let firstVC = storyboard?.instantiateViewControllerWithIdentifier("FViewController")
firstVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)

let secondVC = storyboard?.instantiateViewControllerWithIdentifier("SViewController")
secondVC.tabBarItem = UITabBarItem(tabBarSystemItem: .more, tag: 1)

let thirdVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController")
thirdVC.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 2)

let fourthVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController")
fourthVC.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 3)

   var controllerArray = [firstVC, secondVC]
if UIDevice.current.userInterfaceIdiom == .pad {
      controllerArray += [thirdVC, fourthVC]        
} 
     tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: [=10=])}
    self.view.addSubview(tabBarCnt.view)
}

因此以编程方式创建导航控制器,然后将 FirstVC 或 XVC 指定为该导航控制器的根。已对所有选项卡重复此操作。

因此,在情节提要中,您需要使用导航控制器而不是拖动视图控制器。我附上了截图。

Default it will give TableViewVC as root for Navigation controller