1
votes

I am passing data from a collection view cell when I push to a tab bar controller.

I have a Navigation Controller which I push a segue to a tabbarcontroller. So the TabBarController is in a navigation controller.

The data pass works excellent with this code which is in my prepare to segue.

let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath
        let row = indexPath.row
        let tabar = segue.destinationViewController as! ResultsDetailViewController
        tabar.photoData = photoDataStore[row]

I subclass the TabBarController with a variable of photoData. So all child view controllers work excellent.

But what I am trying to do is if photo2 is nil, then remove the 3rd view controller of the tabbarcontroller. I got this code from another post. Issue is self.tabbarcontroller is returning nil. Can't figure out why?

    if let tabBarController = self.tabBarController {
        let indexToRemove = 2
        if indexToRemove < tabBarController.viewControllers?.count {
            var viewControllers = tabBarController.viewControllers
            viewControllers?.removeAtIndex(indexToRemove)
            tabBarController.viewControllers = viewControllers
        }
    }

As the tabbarcontroller was developed through storyboard I did not set it up in appdelegate.

In the end I want the number of tabbar buttons and hence the number of view controllers to be dynamic.

Thanks

1
How you are setting up your tabor controller in storyboard, please add a screenshotJohnykutty

1 Answers

3
votes

ok found the problem... with the custom Tabbarcontroller when I was doing self.tabbarcontroller which was returning nil... self is already the tabbarcontroller... so instead i just used self.viewcontrollers working code below.

        if var tabBarController = self.viewControllers {
            let indexToRemove = 2
            if indexToRemove < tabBarController.count {
                tabBarController.removeAtIndex(indexToRemove)
                self.setViewControllers(tabBarController, animated: false)
            }
        }