-1

On iOS, why am I getting the following error even though I assigned UserProfile to my StoryboardId:

Use of undeclared type 'UserProfile'

Here's my code:

let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)

 guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "UserProfile") as? UserProfile else {
            return
        }

 navigationController?.pushViewController(destinationVC, animated: true)
Cœur
  • 37,241
  • 25
  • 195
  • 267
marrrrrrk
  • 65
  • 9

1 Answers1

1
 guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "YourStoryBoardID") as? YourViewControllerClassName else {
            return
        }

as? YourViewControllerClassName You need to pass the name of the View Controller not the Storyboard ID here.

UPDATE:

As your current view controller is not embedded in navigationController, navigationController?.pushViewController this returns nil and the line is not executed. To execute this line, your view controller should have a navigation controller.

To embed your controller in navigation Controller : Follow point no:1 ,2 ,3 here

If you do not want to use navigationController, you can use presentViewController instead.

guard let destinationVC = mainStoryboard.instantiateViewController(withIdentifier: "YourStoryBoardID") as? YourViewControllerClassName else {
            return
        }
self.present(destinationVC, animated: true, completion: nil)

Also learn about when to push and when to present a view controller

Keshu R.
  • 5,045
  • 1
  • 18
  • 38
  • after changing it, there is no error but nothing happens on the segue – marrrrrrk Dec 28 '19 at 06:53
  • have you assigned the storyboardId and name of view controller correctly in your storyboard? Attach an image if possible. Is your current controller in which you have placed this code is embedded in a navigation Controller? – Keshu R. Dec 28 '19 at 06:55
  • i believe it is added properly but how do you embed a new controller in the navigation controller? – marrrrrrk Dec 28 '19 at 06:58
  • Follow point no:1 ,2 ,3 here : https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html – Keshu R. Dec 28 '19 at 07:01
  • Thank You the update worked! Do you know if its possible to do it where the new view controller is not stacked on top of the previous one? – marrrrrrk Dec 28 '19 at 07:15
  • Check my answer here: https://stackoverflow.com/a/59499708/8374890 Do upvote it if it helps :) – Keshu R. Dec 28 '19 at 07:17