I currently have a DetailViewController
segued from a ViewController
, which is embedded in a UINavigationViewController
, which I want to embed in a UITabBarController
. When I first did it on my storyboard
, my app crashed with the error:
"Could not cast value of type 'UITabBarController' (0x10badf258) to 'UINavigationController' (0x10badf208)".
After research, I added the first two lines (let tabVc =, and let navVc = )
and still crashed. What am I missing to create a successful TabBarController
?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let tabVc = segue.destinationViewController as! UITabBarController
let navVc = tabVc.viewControllers!.first as! UINavigationController
if segue.identifier == "ShowItem" {
if let row = tableView.indexPathForSelectedRow?.row {
let item = itemStore.allItems[row]
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.item = item
detailViewController.imageStore = imageStore
}
}
}
UPDATE: After applying changes, my error has changed to
"Could not cast value of type 'UITabBarController' (0x103ff6258) to 'Photomania.ItemsViewController' (0x1029520d0)."
As the error clearly states, it's this line of code that is failing:
let navVc = tabVc.viewControllers!.first as! UINavigationController
according to the error the first viewController in the tabVC is not a navigationVC.
So make sure your tab bars are arranged in the correct order and be certain that the first viewController of your tabVC is indeed a navigation view controller.
I think you are approaching the problem in a wrong way. You can set the imageStore
and itemStore
of ItemsViewController
in it's viewDidLoad
.
override func viewDidLoad() {
super.viewDidLoad()
self.itemStore = ItemStore()
self.imageStore = ImageStore()
}
But if you wish to do it in didFinishLaunchingWithOptions
of AppDelegate
then this is how you would do it.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let rootController = window?.rootViewController
if rootController is UITabBarController {
let firstTabItem = (rootController as! UITabBarController).viewControllers?[0]
if firstTabItem is UINavigationController {
let firstController = (firstTabItem as! UINavigationController).viewControllers.first as! ItemsViewController
firstController.itemStore = ItemStore()
firstController.imageStore = ImageStore()
}
}
}
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.