"Has no segue with identifier" 只有在 `tableView (_: didSelectRowAt:)` 上调用 performSegue 时才会出现错误

"Has no segue with identifier" error occurs only when performSegue is called on `tableView (_: didSelectRowAt:)`

我整天都在努力修复 "has no segue with identifier" 错误,但卡住了。

错误信息:

SegueTest[41862:1585831] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Receiver (<SegueTest.TableViewController: 0x7feed6510260>) has no segue with identifier 'tableToCollectionSegue''
*** First throw call stack:

相关文件出现错误:

请在 GitHub 上查看我的 Main.storyboard

Xcode project files on Github

Segue diagram

Capture of segue configuration for ViewController ( Working)

Capture of segue configuration for TableViewController (❌ Getting error)

环境:

我在Main.storyboard中做了什么:

  1. 已创建 UINavigationController 作为初始视图控制器并连接到 RootViewController 作为根视图控制器

  2. 创建从 RootViewController 打开的 UINavigationController 作为模态视图

  3. Open Modal 按钮与模式 UINavigationController 连接起来,并将其命名为 navigationSegue

  4. 创建 ViewControllerTableViewControllerCollectionViewController

  5. 将模式 UINavigationControllerViewController 作为根视图控制器连接

  6. ViewControllerTableViewController 连接为名为 viewToTableSegue

  7. 的触发转场(手动)
  8. TableViewControllerCollectionViewController 连接为名为 tableToCollectionSegue

  9. 的触发转场(手动)
  10. ViewController 上的 Next 按钮连接到 ViewController.swift

  11. 中声明的 @IBAction func didTapNext(_ sender: Any)

错误是怎么产生的:

当我在 ViewController.swift 中调用 performSegue(withIdentifier:sender:) 时,它起作用了。 (Segue capture)

// Go to TableViewController
@IBAction func didTapNext(_ sender: Any) {
    //  Working
    self.performSegue(withIdentifier: "viewToTableSegue", sender: nil)
}

当我在TableViewController.swift中调用performSegue(withIdentifier:sender:)时,出现了错误。 (Segue capture)

override func tableView(_ tableView: UITableView, 
                        didSelectRowAt indexPath: IndexPath) {
    self.tableView.deselectRow(at: indexPath, animated: true)

    // ❌ The segue `tableToCollectionSegue` is connected
    //    from `TableViewController` to `UICollectionViewController`,
    //    but got error.
    //
    // *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
    // reason: 'Receiver (<SegueTest.TableViewController: 0x7feeea04a090>) has no segue with identifier 'tableToCollectionSegue''

    self.performSegue(withIdentifier: "tableToCollectionSegue", sender: nil)

    // ❌ The segue `tableToCollectionSegue` is not connected
    //    from `UINavigationController` to `UICollectionViewController`.
    //    This error is expected.
    //
    // *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
    // reason: 'Receiver (<SegueTest.TableViewController: 0x7feeea04a090>) has no segue with identifier 'tableToCollectionSegue''

    // self.navigationController?.performSegue(withIdentifier: "tableToCollectionSegue", sender: self)
}

我阅读的内容:

我试过的:

TableViewController 中删除以下行:

required public init?(coder aDecoder: NSCoder) {
    super.init(style: .plain)
}