Tushar Sharma
Tushar Sharma

Reputation: 2882

Move from one tab bar to another on click (Tab bar with navigation controllers)

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

Upvotes: 1

Views: 1382

Answers (1)

Maulik Pandya
Maulik Pandya

Reputation: 2230

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 :)

Upvotes: 4

Related Questions