我的swift代码有问题。问题是我需要将prepareForSegue放在方法外部,因此方法内部的数据不能在prepareForSegue外部和内部使用。如何使用外部数据,以便在我的prepareForSegue中工作?
我把我所有的代码都给你了,因为它可以全部使用:-)
错误在代码底部用///---//////向下标记。上面写着:使用未解析的标识符“metadataObj”。

import UIKit
import AVFoundation

class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    @IBOutlet weak var messageLabel:UILabel!

    var captureSession:AVCaptureSession?
    var videoPreviewLayer:AVCaptureVideoPreviewLayer?
    var qrCodeFrameView:UIView?
$$$$$$$$ var metadataObj: AVMetadataMachineReadableCodeObject?

    let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

    override func viewDidLoad() {
        super.viewDidLoad()


        let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        do {

            let input = try AVCaptureDeviceInput(device: captureDevice)
            captureSession = AVCaptureSession()
            captureSession?.addInput(input)

            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

            captureMetadataOutput.metadataObjectTypes = supportedBarCodes

            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = view.layer.bounds
            view.layer.addSublayer(videoPreviewLayer!)


            captureSession?.startRunning()


            view.bringSubviewToFront(messageLabel)


            qrCodeFrameView = UIView()

            if let qrCodeFrameView = qrCodeFrameView {
                qrCodeFrameView.layer.borderColor = UIColor.greenColor().CGColor
                qrCodeFrameView.layer.borderWidth = 2
                view.addSubview(qrCodeFrameView)
                view.bringSubviewToFront(qrCodeFrameView)
            }

        } catch {

            print(error)
            return
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]?, fromConnection connection: AVCaptureConnection!) {


        if metadataObjects == nil || metadataObjects!.count == 0 {
            qrCodeFrameView?.frame = CGRectZero
            messageLabel.text = "No barcode/QR code is detected"
            return
        }


        let metadataObj = metadataObjects![0] as! AVMetadataMachineReadableCodeObject


        if supportedBarCodes.contains(metadataObj.type) {

            let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj)
            qrCodeFrameView?.frame = barCodeObject!.bounds

            if metadataObj.stringValue != nil {
                dispatch_async(dispatch_get_main_queue()){
                    self.performSegueWithIdentifier("SendDataSegue", sender: self)

                }
            }
        }
    }



    if let metadataObj = metadataObj {
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SendDataSegue" {
            if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
                sendToDetailViewController.viaSegue = metadataObj.stringValue

            }
        }
    }
}

}

希望你们能帮我。

最佳答案

您的metadataObjcaptureOutput()函数中声明。你需要把声明移到外面,和你的captureSession一样的地方。
例如:

var metadataObj: AVMetadataMachineReadableCodeObject?

然后您可以在prepareForSeguecaptureOutput()中使用它。在prepareForSegue中使用前检查是否为零:
if let metadataObj = metadataObj {
   // use metadataObj
}

关于ios - 在Swift中通过方法访问数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39258066/

10-11 22:23