Segue error: due to uncaught exception 'NSUnknownKeyException'

Segue error: due to uncaught exception 'NSUnknownKeyException'

我正在尝试通过按下按钮来打开 Segue。在我添加此代码之前,它可以正常工作,但在添加代码(通过单击按钮触发视频的代码)后,代码使我的应用程序崩溃并出现以下错误:

due to uncaught exception 'NSUnknownKeyException reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fatB.

我搜索了这个错误,我明白这意味着 Xcode 找不到某个名字 (fatB) 但我想不出剩下的。

下面是我的代码:

viewcontroller1代码:

import UIKit

class ViewController: UIViewController {
    @IBAction func movieB(_ sender: Any) {
        performSegue(withIdentifier: "firstsegue", sender: self)
    }

    @IBOutlet weak var winnerLabel: UILabel!
}

viewcontroller2代码:

import UIKit
import AVKit
import AVFoundation

class ViewController2: UIViewController {

    func forbutton(name : String, type : String)  {
        func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            playVideo(name: name, type: type)
        }
    }

    private func playVideo(name : String, type : String) {
        guard let path = Bundle.main.path(forResource: "\(name)", ofType:"\(type)") else {
            debugPrint("video not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        let playerController = AVPlayerViewController()
        playerController.player = player
        present(playerController, animated: true) {
            player.play()
        }
    }        

    @IBAction func fatB(_ sender: Any) {
        forbutton(name: "fatB", type: "mp4")
    }

    @IBAction func coderB(_ sender: Any) {
        forbutton(name: "birdC", type: "mp4")
    }

    @IBAction func clashB(_ sender: Any) {
        forbutton(name: "clash", type: "mp4")
    }
}

检查连接检查器。有一个名为 fatB 的 IBOutlet。从那里删除它

它可能是一个 UIButton

可能是因为:

1) 您已从代码中删除了 IBOutlet,但未从 Storyboard 中的 Controller's scene 中删除。

2) 您可能更改了 IBOutlet 引用变量的名称。

请检查 Storyboard 中的 Controller's scene 并删除未使用的 IBOutlet 引用或使用新的引用变量名称更新它。