0

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
        }
    }
}

Storyboard

UPDATE: After applying changes, my error has changed to

"Could not cast value of type 'UITabBarController' (0x103ff6258) to 'Photomania.ItemsViewController' (0x1029520d0)."

Error

Allie
  • 57
  • 7
  • Check my answer on the link http://stackoverflow.com/questions/40338691/how-to-reuse-header-section-all-scene/40352965#40352965 and follow the step 1 from the answer..hope this helps... – Joe Nov 01 '16 at 23:08
  • Thank you for the help but that doesn't exactly apply to the error I am receiving. Any other suggestions? – Allie Nov 02 '16 at 02:46
  • Can you post your storyBoard hierarchy..you getting that error probable you haven't config the navigationController to your tabBarController or your not using navigationController at all or problem with setting segue identifier to your tabBarController etc....? – Joe Nov 02 '16 at 02:53
  • You have your answer in your hand mate. if you look at my answer on that post properly.. – Joe Nov 02 '16 at 02:54
  • @Allie which __class/viewController__ does this code belong to that you have provided? – Adeel Miraj Nov 02 '16 at 07:17
  • @Joe Here is my story board hierarchy after I followed your instructions.It is at the bottom of my question. – Allie Nov 02 '16 at 14:24
  • @Adeel it belongs to my ItemsViewController, the VC for my first tab. It also has a detailVC attached to it. – Allie Nov 02 '16 at 14:28
  • @Joe Please see my update at the bottom of my question. – Allie Nov 02 '16 at 14:32
  • is your app crashes after build or after segue...let me know which one is 'Photomania.ItemsViewController from your storyboard – Joe Nov 02 '16 at 14:45
  • @Allie you are only telling what is happening. You are not telling anything about what you want. – Adeel Miraj Nov 02 '16 at 14:45
  • @Adeel I want to embed this navigation controller into a tab bar controller, but when I do I am receiving an error. – Allie Nov 03 '16 at 00:52
  • @Allie the first screenshot in your question seems correct the second isn't because in that the `TabBarController` is embedded in a `NavigationController`. – Adeel Miraj Nov 03 '16 at 13:43
  • @Adeel That is how Joe told me to solve it. If you were to embed a NavigationController into a TabBarController, how would you go about it? I cannot figure out what I am missing. – Allie Nov 03 '16 at 13:54
  • As I said your first screenshot is perfect. Now tell me what problem are you facing? – Adeel Miraj Nov 03 '16 at 14:10
  • Your navigation controller is already in the tab bar controller. Now what do you want to do? – Adeel Miraj Nov 03 '16 at 14:10
  • @Adeel It will not run. There is an error that says "Could not cast value of type 'UITabBarController' (0x103ff6258) to 'Photomania.ItemsViewController' (0x1029520d0)." – Allie Nov 08 '16 at 14:43
  • At which line does this error occur? – Adeel Miraj Nov 08 '16 at 14:47
  • @Adeel I added the picture at the bottom of my question. It is in my app delegate, where I configured my original structure. I am just now adding the tab bar, it used to be only the navigation controller and the corresponding view controllers. – Allie Nov 08 '16 at 15:06
  • Your rootViewController is not a UINavigationController instead it is a UITabBarController. So this error is obvious. I don't understand why are you setting imageStore and itemStore of ItemsViewController here. You can do this in the viewDidLoad of ItemsViewController. – Adeel Miraj Nov 08 '16 at 15:23
  • @Adeel What exactly do I need to change/write? I am new to this and was following a tutorial. – Allie Nov 08 '16 at 15:24
  • Allie please check my answer below. I hope it'll fix your problem. – Adeel Miraj Nov 08 '16 at 16:11

2 Answers2

0

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.

Wink
  • 187
  • 7
  • My tab bars are in the correct order and the first viewController is a nav view controller. The same error continues to send. – Allie Nov 02 '16 at 02:45
0

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()
        }
    }
}
Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
  • I think this will work, except I am getting a strange error about the curly braces. I know that they all match up but the compiler is saying otherwise. See picture. – Allie Nov 08 '16 at 17:19
  • Which picture are you referring to? And yes you are right that the curly braces do match up. – Adeel Miraj Nov 08 '16 at 17:24
  • I just added the picture to my original question too. – Allie Nov 08 '16 at 17:29
  • Yeah I saw the picture and it's really strange because there's no issue with the braces. Just clean the project and try to build. Or you can simply comment out the function that's throwing this error. – Adeel Miraj Nov 08 '16 at 17:32
  • How do I do that? I do not see anything. – Allie Nov 09 '16 at 14:31
  • I ran into another major issue. I may have to change the organization of my app. – Allie Nov 09 '16 at 14:51
  • Take a [tour](http://stackoverflow.com/tour) to learn how to to accept an answer and upvote it. – Adeel Miraj Nov 09 '16 at 15:30