自定义条形码扫描仪,无法传回扫描数据

时间:2019-01-21 23:24:54

标签: ios swift barcode

我无法传回由自定义条形码扫描仪扫描的数据。 数据已成功读取,我可以将值分配给变量。但是我无法将数据传递回上一个视图控制器以填充文本视图。

我在下面使用它传递给我的条形码VC,以在其中存储数据

var barcodeScanData: String = ""

我在下面使用

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "BarcodeScanVC" {
        let desitnationVC = segue.destination as! BarcodeScanVC
        desitnationVC.xyz = barcodeScanData
    }
}

下面是我尝试从自定义条形码扫描仪发送回数据的地方

var xyz: String = ""

func launchApp(barcodeScan: String) {

        if presentedViewController != nil {
            return
        }

        let alertPrompt = UIAlertController(title: "Barcode Found", message: "\(barcodeScan)", preferredStyle: .actionSheet)
        let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertAction.Style.default, handler: { (action) -> Void in
            let barcodeData = PartsVCDetail()
            self.xyz = barcodeScan
            barcodeData.barcodeScanData = self.xyz
            print(self.xyz, "This is what I am sending")
            print(barcodeData.barcodeScanData, "This is what I am sending it TO" )


            self.navigationController?.popViewController(animated: true)
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)

        alertPrompt.addAction(confirmAction)
        alertPrompt.addAction(cancelAction)

        present(alertPrompt, animated: true, completion: nil)
    }

两条打印线

print(self.waybill, "This is what I am sending")
print(barcodeData.barcodeScanData, "This is what I am sending it TO"

但是,当我使用下面的最后一行时,请向我显示正确的扫描数据:

self.navigationController?.popViewController(animated: true)

数据丢失,并且在第一个视图控制器上的viewDidAppear中看到一个空值:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    print(barcodeScanData, "This is empty but it shouldnt be")
    dataFromBarcodeScanner.text = barcodeScanData
}

我想念什么?

1 个答案:

答案 0 :(得分:1)

使用这段代码let barcodeData = PartsVCDetail(),您将创建一个PartsVCDetail的新实例,然后设置该实例的属性。操作结束后,该实例将被释放,您将通过popViewController返回上一个视图控制器。

一种常见的解决方案是委托模式。

  • 您为委托人声明要实施的协议
  • 您有原始的视图控制器实现此委托协议
  • 您已将原始视图控制器设置为第二个视图控制器的委托
  • 第二个视图控制器可以调用委托方法以将数据传回

协议

protocol BarcodeScanDelegate {
    func didScan(barcodeData: String)
}

PartsVCDetail

class PartsVCDetail: UIViewController, BarcodeScanDelegate {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let desitnationVC = segue.destination as? BarcodeScanVC {
            desitnationVC.delegate = self
        }
    }

    func didScan(barcodeData: String) {
        self.barcodeScanData = barcodeData
    }
}

BarcodeScanVC

var delegate: BarcodeScanDelegate?

func launchApp(barcodeScan: String) {

    guard presentedViewController == nil else {
        return
    }

    let alertPrompt = UIAlertController(title: "Barcode Found", message: "\(barcodeScan)", preferredStyle: .actionSheet)
    let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertAction.Style.default, handler: { (action) -> Void in
        self.delegate?.didScan(barcodeData: self.xyz)
        self.navigationController?.popViewController(animated: true)
    })

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)

    alertPrompt.addAction(confirmAction)
    alertPrompt.addAction(cancelAction)

    present(alertPrompt, animated: true, completion: nil)
}