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