1

I have tabbar controller which done programmatically. And tab bar is working fine. In stroyboard, i have created firstViewController and assigned class name firstViewController. When i tried to tab on first vc on tab bar controller to navigates to first viewcontroller and it crash. if i give programmatically mean it works fine.

How to navigate from programmatically tab bar controller to storyboard view controller.

here is my code of tab bar controller:

 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: $0)}

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

        self.view.addSubview(tabBarCnt.view)
    }

}

Here is my firstVC of firstViewController code:

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


}
PvUIDev
  • 95
  • 1
  • 9
  • 38

2 Answers2

4

Please try below code. you can give view controller identifier by interface builder.

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: $0)}
    self.view.addSubview(tabBarCnt.view)
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Dixit Rathod
  • 177
  • 5
0

So programatically navigation controller is created and then assigned FirstVC or XVC as root for that navigation controller. This action has been repeated for all tabs.

So, in storyboard instead of dragging view controller you need to take navigation controller. I have attached screenshots.

Default it will give TableViewVC as root for Navigation controller

enter image description here enter image description here

Niki
  • 1,566
  • 1
  • 19
  • 36