简体   繁体   中英

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 let barViewControllers = self.tabBarController?.viewController[1] as? myController to pass data.

But this is the line that gives me above crash

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM