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

标签 swift xcode storyboard segue xcode10.1

我整整一天都在努力修复“没有带标识符的 segue”错误,但卡住了。

错误信息:

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)

环境:

  • macOS Mojave 10.14.2
  • Xcode 10.1
  • swift 4.2

我在 Main.storyboard 中做了什么:

  1. 创建 UINavigationController 作为初始 View Controller 并连接到 RootViewController 作为 Root View Controller

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

  3. Open Modal 按钮连接到模态 UINavigationController 并将其命名为 navigationSegue

  4. 创建 ViewControllerTableViewControllerCollectionViewController

  5. 将模态 UINavigationControllerViewController 作为 Root View Controller 连接

  6. ViewControllerTableViewController 连接为名为 viewToTableSegue

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

    的触发转场(手动)
  8. ViewController 上的 Next 按钮与 中声明的 @IBAction func didTapNext(_ sender: Any) 连接起来>ViewController.swift

错误是如何发生的:

当我在 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)
}

我读过的内容:

我尝试过的:

  • 确保正确指定并实际连接了 segue 标识符
  • 重置所有模拟器设置和内容
  • 清理 Xcode 构建文件夹和 ~/Library/Developer/Xcode/DerivedData/ 目录
  • 重命名 Storyboard
  • 重新启动并重建 Xcode 项目
  • 重启苹果

最佳答案

TableViewController 中删除以下行:

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

关于swift - 只有在 "Has no segue with identifier"上调用 performSegue 时才会出现 `tableView (_: didSelectRowAt:)` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54123322/

相关文章:

ios - Tab Bar Controller 中 TabBar 项目的不同颜色

Swift 3 在解包 Optional 值时意外发现 nil

ios - 密码保护 iPhone 应用程序

Xcode 签名版本提示开发证书

ios - 如何使用 didSelectRowAtIndexPath(在 Swift 中)在两个 Storyboard 之间传递日期?

ios - 在 NSUserDefaults 中保存一个元组

html - 如何在 Swift 中动态加载 HTML

iphone - 在 Xcode 中删除/移除组

ios - 将边距添加到自定义 UI 表格单元格

ios - 我想更改 UINavigationBar 的高度并添加标题..为什么这么难?