I am trying to make a template for my app... lets say my app loads the same view controller which has a CollectionView for 4 tabs. According to selected index, I have to load the contents into collection view. I am setting up the tab bar manually from Appdelegate. My question is Is this possible like instantiating same viewcntroller for all 4 tabs of Tabbarcontroller at a time. if yes, how will i know correctly that which index is selected?
Code for tabBarcontroller in Appdelegate
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tabBarController = UITabBarController()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let firstImage = UIImage(named: "image1")
let secondImage = UIImage(named: "image2")
var controllers = [UIViewController]()
for var i = 0; i < self.myList.count; i++ {
let vc : ViewControllerTwo = storyboard.instantiateViewControllerWithIdentifier("view1") as! ViewControllerTwo
if(i == 0 || i == 3)
{
vc.tabBarItem = UITabBarItem(
title: self.myList[i],
image: firstImage,
tag: i)
controllers.append(vc)
}
else
{
vc.tabBarItem = UITabBarItem(
title: self.myList[i],
image: secondImage,
tag: i)
controllers.append(vc)
}
}
self.tabBarController.viewControllers = controllers
self.window?.rootViewController = self.tabBarController
self.self.window?.rootViewController = self.tabBarController
self.window?.makeKeyAndVisible()