0

I want to switch between different view controllers, here is my codes,

let sb = UIStoryboard(name:"Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("tabBarController") as ViewController 
self.presentViewController(vc, animated: true, completion: nil)

'tabBarController' is the storyboad ID I had wrote in the identifier inspector.but I got some error at this line.

let vc = sb.instantiateViewControllerWithIdentifier("tabBarController") as ViewController 

and here is the screenshot, swift_dynamicCastClassUnconditional

What's the problem?

And I have another question, here is the screenshot after the sb was initialized, the storyboardFileName is "Main.storyboardc", shouldn't it be "Main.storyboard"?

enter image description here
Thanks very much!!!!

1 Answer 1

1

The error is saying that the view controller returned by instantiateViewControllerWithIdentifier cannot be cast to a ViewController.

This is probably because you did not set the Custom Class property of your view controller in the storyboard to ViewController.

Go to your storyboard, select your view controller, then open the Identity inspector and look for the Custom Class property. Set it to ViewController and tab out of the field to make sure it takes. Then run again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I changed the "ViewController" to "UITabBarController" , and it works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.