-1

I'm working on my first iOS project without a storyboard, and I'm running into this issue while trying to segue to another View Controller. Would really appreciate help to understand why this issue is occurring. Do I need to define the segue somewhere? Thank you!

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'goToYViewController''

This is what I have:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "goToYViewController") {
        if let yInfo = segue.destination as? YViewController {
            yInfo.row = "hello"
        }
    }
}

// in data source

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "goToYViewController", sender: self)
}
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
HelloWorld
  • 370
  • 1
  • 7
  • 18
  • “without a storyboard“ if there is no storyboard, there are no segues, and so none of your code makes any sense. – matt Jul 11 '19 at 05:15
  • 2
    [Create and perform segue without storyboards](https://stackoverflow.com/questions/36216582/create-and-perform-segue-without-storyboards) – Ricky Mo Jul 11 '19 at 05:16
  • see this for help : [Swift programmatically navigate to another view controller/scene](https://stackoverflow.com/questions/39450124/swift-programmatically-navigate-to-another-view-controller-scene/39450328) – Anbu.Karthik Jul 11 '19 at 05:19

2 Answers2

1

Since you're not using the storyboard, you must push/present the YViewController instance programatically like so,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let yInfo = YViewController()
    yInfo.row = "hello"
    self.present(yInfo, animated: true, completion: nil)
}

In the above code, yInfo is being presented. In case you want to push yInfo in navigation stack use pushViewController(_:animated:),

self.navigationController?.pushViewController(yInfo, animated: true)
PGDev
  • 23,751
  • 6
  • 34
  • 88
1

I am afraid you can't use segue here if you don't have a storyboard. You can either use push or present in order to show the second view controller if you want to do it programmatically.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let YVC = YViewController()
    self.present(YVC, animated: true)
}
Vandan Patel
  • 1,012
  • 1
  • 12
  • 23