gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:53:09 25 4
gpt4 key购买 nike

我无法传回由我的自定义条码扫描器扫描的数据。数据已成功读取,我可以将值分配给变量。但是我无法将数据传回之前的 View Controller 来填充 TextView 。

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

var barcodeScanData: String = ""

我在下面使用 prepare for segue

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)

数据丢失,我在第一个 View Controller 的 viewDidAppear 中看到一个空值:

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

我错过了什么?

最佳答案

使用此代码 let barcodeData = PartsVCDetail() 您将创建一个新的 PartsVCDetail 实例,然后设置该实例的属性。一旦操作结束,该实例将被释放,您将通过 popViewController 返回到之前的 View Controller 。

您的需求的常见解决方案是委托(delegate)模式。

  • 您声明一个协议(protocol)供您的代表实现
  • 您让原始 View Controller 实现此委托(delegate)协议(protocol)
  • 您将原始 View Controller 设置为第二个 View Controller 的委托(delegate)
  • 第二个 View Controller 可以调用委托(delegate)方法将数据传回

协议(protocol)

protocol BarcodeScanDelegate {
func didScan(barcodeData: String)
}

零件VCDetail

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
}
}

条码扫描VC

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)
}

关于ios - 自定义条码扫描仪,无法传回扫描数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54299153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com