ios - 当我尝试从 Storyboard 中继续时,为什么我在这行代码中遇到段错误?

标签 ios swift3 segue xcode8 uistoryboardsegue

因此,我试图将 Storyboard 中的一个按钮连接到另一个 ViewController。但是,每次我点击按钮时,应用程序都会崩溃。这是在应用程序崩溃时突出显示的代码行。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let barcodeViewController: BarcodeReaderViewController = segue.destination as! BarcodeReaderViewController
    barcodeViewController.delegate = self

}

这是我的 BarCodeVC

import UIKit
import AVFoundation

protocol BarcodeDelegate {
  func barcodeReaded(barcode: String)
}

class BarcodeViewController: UIViewController, 
                             AVCaptureMetadataOutputObjectsDelegate {

var delegate: BarcodeDelegate?

var videoCaptureDevice: AVCaptureDevice = 
 AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var device = AVCaptureDevice.defaultDevice(withMediaType: 
  AVMediaTypeVideo)
var output = AVCaptureMetadataOutput()
var previewLayer: AVCaptureVideoPreviewLayer?

var captureSession = AVCaptureSession()
var code: String?

override func viewDidLoad() {
  super.viewDidLoad()

  self.view.backgroundColor = UIColor.clear
  self.setupCamera()
}

private func setupCamera() {

  let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)

  if self.captureSession.canAddInput(input) {
      self.captureSession.addInput(input)
  }

  self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

  if let videoPreviewLayer = self.previewLayer {
      videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
      videoPreviewLayer.frame = self.view.bounds
      view.layer.addSublayer(videoPreviewLayer)
  }

  let metadataOutput = AVCaptureMetadataOutput()
  if self.captureSession.canAddOutput(metadataOutput) {
      self.captureSession.addOutput(metadataOutput)

      metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
      metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
  } else {
      print("Could not add metadata output")
  }
}

 override func viewWillAppear(_ animated: Bool) {
   super.viewWillAppear(animated)

   if (captureSession.isRunning == false) {
      captureSession.startRunning();
   }
}

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)

  if (captureSession.isRunning == true) {
     captureSession.stopRunning();
  }
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
   // This is the delegate'smethod that is called when a code is readed
   for metadata in metadataObjects {
       let readableObject = metadata as! AVMetadataMachineReadableCodeObject
       let code = readableObject.stringValue


       self.dismiss(animated: true, completion: nil)
       self.delegate?.barcodeReaded(barcode: code!)
       print(code!)
   }
 }
}

我的 segue 只是一个简单的拖动来显示,而不是以编程方式。所以我对它崩溃的原因有点困惑。

这是错误:

Could not cast value of type 'BarCodeProj.ViewController' (0x1000361b0) to 'BarCodeProj.BarcodeViewController' (0x100035a78).

最佳答案

在这种情况下,控制台错误会告诉您需要知道的一切。 segue.destination 不是 BarcodeReaderViewController,因此您尝试将其转换为一个(使用 as!)会导致灾难性的失败。

首先,检查您的 Storyboard并确保正确设置 View Controller 的类型(您要转换到的 View Controller 。)

通过在 Storyboard中选择适当的 View Controller 并在此处选中来执行此操作:

location where class name must be entered.

关于ios - 当我尝试从 Storyboard 中继续时,为什么我在这行代码中遇到段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989936/

相关文章:

iphone - Core Animation 和 UIImageView 返回起始位置

ios - Swift 3 将 ArrayList 对象变量传递给字符串数组

iOS 首次启动之旅——检测应用是否首次启动

ios - 由于发送到 VC 的无法识别的选择器导致应用程序崩溃

iOS:在不同 View 上使用 NSUserDefault 数据更新文本标签

ios - 使用两个独立的 View Controller 更新核心数据模型

ios - 无法使用类型为“(快照 : (DataSnapshot)) Swift 3) 的参数列表调用类型 'User' 的初始值设定项

ios - 从导航后退按钮解除 segue

ios - 发现意外的 Mach-O header 代码 : 0x72613c21 on submitting to App Store

swift3 - NotificationCenter 发布以更新主线程上的 UI 元素?