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)]

6
  • You cannot pass class type to pushViewController method. it should be instance of a EndViewController Commented Mar 14, 2018 at 11:58
  • @SHISHIRRAMIN so how the code would be? Commented Mar 14, 2018 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 stackoverflow.com/questions/27374759/… Commented Mar 14, 2018 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 Commented Mar 14, 2018 at 12:05
  • @Nilesh Ok, thank you so much! Commented Mar 14, 2018 at 12:07

1 Answer 1

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.

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

1 Comment

No problem, please feel free to mark as an answer if that does resolve your issue.

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.