KaKa
KaKa

Reputation: 559

Switch between different view controllers using swift

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

Upvotes: 0

Views: 602

Answers (1)

Mike Taverne
Mike Taverne

Reputation: 9352

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.

Upvotes: 1

Related Questions