@已发布未更新视图

时间:2020-07-24 12:17:16

标签: ios swift swiftui

我在应用中使用条形码扫描仪,并在扫描条形码时在视图中显示每个产品。

我有一张纸来显示产品的详细信息,我希望在ScannedCode更新时重新加载它。

对于每个使用条形码的类,我将其声明为:

    @ObservedObject var scannedCode: ScannedCode

但是当我更改值时,视图不会更新。

我在contentView中声明了ScannedCode:

class ScannedCode: ObservableObject {
    @Published var barcode = ""
}
class dbProduct: ObservableObject {
    @Published var result = Result()
}

    struct ContentView: View {
        let scannedCode = ScannedCode()
        let product = dbProduct()
        
        var body: some View {
            ZStack {
                ScannerView(scannedCode: scannedCode) //Starts the scanner
                FoundItemSheet(scannedCode: scannedCode, product: product)
            }
        }
    }

扫描仪找到产品后,会更新其协调器中的条形码:

 class Coordinator: BarcodeScannerCodeDelegate, BarcodeScannerErrorDelegate {
        @ObservedObject var scannedCode: ScannedCode
        
    private var scannerView: ScannerView
        init(_ scannerView: ScannerView, barcode: ScannedCode) {
            self.scannerView = scannerView
            self.scannedCode = barcode
        }
        
        func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
            self.scannedCode.barcode = code //Updates the barcode here
            controller.resetWithError(message: "Error message")
        }

        func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
          print(error)
        }
    }

FoundItemSheet调用BottomSheetView,它显示产品。 productDataView计算哪些数据作为其content()显示在BottomSheetView上。

为BottomSheetView()加载主体时,我调用API并将数据存储到@ObservedObject中,以便productDataView可以访问它。

.onAppear{
        DispatchQueue.main.async {
            let hashedValue = scannedCode.barcode.hashedValue("Ls75O8z1q9Ep9Kz0")
            self.loadData(url: *API Call with barcode*) {
//...Load data converts from JSON and stores the product

这是我怀疑它可能出问题的地方,因为在此处未更新扫描仪协调器中更改的条形码。

编辑:

ScannerView

extension UINavigationController {
   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

struct ScannerView: UIViewControllerRepresentable {
    @ObservedObject var scannedCode: ScannedCode

    func makeCoordinator() -> Coordinator {
        Coordinator(self, barcode: scannedCode)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<ScannerView>) -> BarcodeScannerViewController {
        return createAndConfigureScanner(context: context)
    }

    func updateUIViewController(_ uiViewController: BarcodeScannerViewController, context: UIViewControllerRepresentableContext<ScannerView>) {
            uiViewController.reset(animated: false)
      }

     private func createAndConfigureScanner(context: UIViewControllerRepresentableContext<ScannerView>) -> BarcodeScannerViewController {
        let barcodeVC = BarcodeScannerViewController()
        barcodeVC.codeDelegate = context.coordinator
        barcodeVC.errorDelegate = context.coordinator
        return barcodeVC
    }
}

0 个答案:

没有答案