Swift - Could not cast value of type 'UITabBarController'
Since your segue is connected to a UITabBarController
, you need to cast it to UITabBarController
and get the ViewController object using the viewControllers
property of the tab bar controller.
let tabCtrl = segue.destinationViewController as! UITabBarController
let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController // Assuming home view controller is in the first tab, else update the array index
destinationVC.userObject = self.userObject;
For Swift 4:
let tabCtrl: UITabBarController = segue.destination as! UITabBarController
let destinationVC = tabCtrl.viewControllers![0] as! HomeViewController
destinationVC.userObject = userObject[String!] // In case you are using an array or something else in the object
Could not cast value of type 'UITabBarController' to 'ViewController'
Your viewController is being presented from UITabBarController. With approach you are using I believe you can access it like this (where index is index in UITabBarController of your UINavigationController containing RentVC):
if let tab = self.presentingViewController as? UITabBarController,
let nav = tab.viewControllers?[index] as? UINavigationController,
let rentViewController = nav.viewControllers.first as? RentViewController {
rentViewController.data = data
}
However, I would suggest using a delegate or callback block to pass data in this occassion.
For delegate approach, first create protocol:
protocol PassDataDelegate:class {
func passData(data:YourType)
}
Then in TableViewController:
class TableViewController: UIViewController {
weak var delegate: PassDataDelegate?
}
And in RentViewController:
extension RentViewController: PassDataDelegate {
func passData(data:YourType) {
//use data to suit your needs
}
}
Before presenting TableViewController, in RentViewController, set its delegate:
tableViewController.delegate
present(tableViewController, animated: true)
And finally, inside TableViewController, before dismissing, call delegate's method to pass data:
delegate?.passData(data: <<someData>>)
Could not cast value of type 'UITabBarController' Swift
You didn't set the class of UITabBarController
. Like this:
Updated:
1. Did TabBarVC
class exist?
2. Did TabBarVC
inherit UITabBarController
?
If both is yes,just copy paste TabBarVC
to its class item. And build it!
Could not cast value of type 'UINavigationController to tabBarController
let svc = (barViewControllers![1] as! UINavigationController).viewControllers[0]
I believe all the tab viewControllers have embedded navigation controller. So when you access viewController from tabs viewController you get UINavigationController. In order to access VC you have to get the viewControllers at index 0 of NavigationController
Much cleaner safer code
if let vc = (self.tabBarController.viewControllers![0] as? UINavigationController)?.viewControllers[0] as? CategoriesViewController {
//access your VC here
}
Could not cast value of type 'UITabBarController' (0x1079d85d8) to 'App.DetailViewController'
I think this issues produce In that case you Contact the NavigationController in DetailViewController after the You Push the NavigationController of DetailViewController 'Pushing a navigation controller is not supported'
Your ViewController hierarchy wrong in TabBarView Application
1. any Controller Connected which have as First ViewController with UITabbarController (RootViewController EmbedIn UINavigationController) after the DetailViewController Connect With Button Segue or push to Button's Action then App Properly working
2. List item
But When you Present Detail View Controller then no have any crashes received. app properly work
Could not cast value of type 'UINavigationController' (0x10836e698) to 'UITabBarController' (0x10836e6e8).?
Your first line:
var mainCont : UITabBarController = ((UIApplication.sharedApplication().delegate)
as! AppDelegate).window?.rootViewController as! UITabBarController
Is getting the window's rootViewController
as UITabBarController
. And the error message is really kind of clear:
Could not cast value of type 'UINavigationController' (0x10836e698) to 'UITabBarController' (0x10836e6e8).
On app start the window's rootViewController is set to what ever you have defined as your initial view controller on the Storyboard (The big grey arrow). My guess is that your initial view controller is set to a UINavigationController
, but in your code you are trying to cast (force) it to be a UITabBarController
.
fix the error - Could not cast value of type 'SecondViewController' to 'FirstViewController'
I fixed this error in a second after I swapped this 2 icons using drag and drop.
Thank you for your time !
Related Topics
Restkit, Coredata and Swift - I Can't Seem to Fetch Results Back Out
Saving Contact Address to Unified Contact Results in (Cnerrordomain Error 500)
Please Specify a Platform for This Target in Your Podfile
In Collectionview How to Set Colors According to Selection
Auto Focus and Auto Exposure in Avfoundation on Custom Camera Layer
iOS Swift3 Check Nil Value for Viewcontroller Object
Supporting a Nsmanagedobject Fetchrequest() Class Method in iOS 9 and 10
Endless Scrolling (Repeating) Background in Spritekit Game - Swift
Different Cell in Tableview Swift 3
How to Get All the Sundays in Array of Date iOS
Get All List of Uiviewcontrollers in iOS Swift
Simplest Way to Implement a "Read More" Button to Expand a Uitextview in iOS Swift 2
How to Convert an Int into Nsdata in Swift
How to Read Heart Rate from iOS Healthkit App Using Swift
Swift Uinavigation Bottom Line and Shadow Remove Without Navbar Color Change
Wkwebview Blank After 'Successful' Https Nsurlrequest
Setting a Stateobject Value from Child View Causes Navigationview to Pop All Views