1

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()
Saty
  • 2,563
  • 3
  • 37
  • 88
  • I think I resolved it using a method with a NSTimer as sometimes the change and get states gives bad state as described here http://stackoverflow.com/questions/28099148/switch-tab-bar-programatically-in-swift – Saty Apr 25 '16 at 05:52
  • You should set your class as the delegate for the tab bar controller and then you will get a call to `didSelectViewController`. You can compare the selected view controller with the `controllers` array to determine the index of the selected view controller – Paulw11 Apr 25 '16 at 06:07
  • You should not need an NSTimer unless you are doing something wrong... – Paulw11 Apr 25 '16 at 06:08
  • @Paulw11..can you provide some sample code? – Saty Apr 25 '16 at 07:34
  • For what? It isn't clear what your problem is. – Paulw11 Apr 25 '16 at 07:38
  • can you provide some code where i have to set my class as delegate and when I change the tab, i will get the tab selected index.! – Saty Apr 25 '16 at 07:41

1 Answers1

2

If you set your class as the delegate for your tab bar controller, you will get a call to the didSelectViewController delegate method. You can then use your controllers array to determine the index;

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tabBarController = UITabBarController()
tabBarController.delegate = self


func tabBarController(_ tabBarController: UITabBarController,
   didSelectViewController viewController: UIViewController) {

    if let index = self.controllers.indexOf(viewController) {
        // Do something with index
    }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186