1

Hi all I want to segue from first tabbarcontroller to second tabbarcontroller but not able to do so. View Controllers in my TabBar are also embedded with NavigationControllers ,and so when i try to switch tabs it gives me error saying-:

Could not cast value of type 'UINavigationController' (0x1048d5898) to 'abc.CategoriesController' (0x101088d88).

Code I used-:

 let barViewControllers = self.tabBarController?.viewController
 let svc = barViewControllers![1] as! myController
 svc.myOrder = self.myOrder

I tried many solutions but failed to segue can anyone help me? Please explain me concept behind this if anyone can?

self.tabBarController.selectedIndex = 1 does work but I can not pass data with this method.

As Maddy said-:

Use let barViewControllers = self.tabBarController?.viewController[1] as? myController to pass data.

But this is the line that gives me above crash

Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
  • error suggest on the [1] position of that tabbatcontroller you are getting is UINavigationController you need to find the all stack of that navigationcontroller – Maulik Pandya Jul 18 '17 at 17:03
  • don't cast your [1] to myController cast it to UINavigationController and after what svc.viewControllers gives your all stack viewControllers of navigationcontroller – Maulik Pandya Jul 18 '17 at 17:09

1 Answers1

4

The error you got suggest that what you getting from the below code is not the viewController if you

let barViewControllers = self.tabBarController?.viewController //some what wrong with this

best way to access all view controller is as below

var svc = self.tabBarController.viewControllers

and if you want to access viewControllers by index of them then below one

var svc = self.tabBarController.viewControllers[1] as yourVC

and last but not in list if you want to access navigation controller from the tabbar

var svc = self.tabBarController.viewControllers[1] as UINavigationController

Hope this help you :)

Maulik Pandya
  • 2,200
  • 17
  • 26
  • that is not giving crash but not even switching tabs and moreover with this last step how will I send data to specific controller. – Tushar Sharma Jul 18 '17 at 16:32
  • you can pass the data using var svc = self.tabBarController.viewControllers[1] as yourVC and for switching the tab you can go with self.tabBarController.selectedIndex and make sure all you chasing up with the viewWillappear of you destination tab – Maulik Pandya Jul 18 '17 at 16:40