0

I'm trying to create an app which will switch programmatically from a View Controller to another. I've already tried to do that with this code:

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "EndViwController") as! EndViewController
self.navigationController?.pushViewController(EndViewController, animated: true)

But it turns me this error:

[Cannot convert value of type 'EndViewController.Type' to expected argument type 'UIViewController']

Also, when I try this other code:

let timeLineTableVC = EndViewController()
self.present(timeLineTableVC, animated: true, completion: nil)

It gives to me a black screen on the simulator and the EndViewController doesn't appear.

In the end, when I try this:

let storyboard = UIStoryboard(name: "EndViewController", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "EndViewController")
self.present(controller, animated: true, completion: nil)

It gives me this error:

[libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)]

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mules
  • 13
  • 3
  • You cannot pass class type to pushViewController method. it should be instance of a EndViewController – SHISHIR R AMIN Mar 14 '18 at 11:58
  • @SHISHIRRAMIN so how the code would be? – Mules Mar 14 '18 at 12:04
  • In UIStoryboard name you have to pass your storyboard name not controller name, Please cross verify once your storyboard name and controller name also you have to assign storyboard identifier in your EndViewController look this url https://stackoverflow.com/questions/27374759/programmatically-navigate-to-another-view-controller-scene – Nilesh Mar 14 '18 at 12:04
  • @SHISHIRRAMIN I’m sorry for asking this stupid question but I’m learning swift so I’m having a lot of problems – Mules Mar 14 '18 at 12:05
  • @Nilesh Ok, thank you so much! – Mules Mar 14 '18 at 12:07
  • @Mules please check your EndViewController "class name" and "storyboard identifier" and make "true use storyboard id" in UIStoryboard... i think you forgot to assign the name of class for your EndViewController. – Bandish Dave Mar 14 '18 at 13:21

1 Answers1

3

Your first bit of code doesn't work because EndViewController is a type. You should replace this with secondViewController that you declared above.

The second bit of code doesn't work because you are creating an 'empty' EndViewController type and not instantiating the storyboard view .

I'm guessing the third bit of code is failing because your storyboard isn't called "EndViewController" (unless you've renamed it). Try changing this to "Main" instead.

Chris Edgington
  • 2,937
  • 5
  • 23
  • 42