【发布时间】:2026-01-31 04:10:01
【问题描述】:
我正在我的应用程序中构建一个 QR 码阅读器,到目前为止,我将它作为工作表打开并在检测到 qr/条形码时关闭。 应用程序的阅读器部分使用 UIKit,我有文件 QRCodeScan.swift,它是 UIViewControllerRepresentable,qr 扫描仪返回它在此文件中的协调器中找到的代码的值。 我似乎找不到任何方法将找到的代码从协调器中取出到原始视图中。
这是文件 QRCodeScan。
struct QRCodeScan: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> ScannerViewController {
let vc = ScannerViewController()
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ vc: ScannerViewController, context: Context) {
}
class Coordinator: NSObject, QRCodeScannerDelegate {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
func codeDidFind(_ foundCode: String) {
print(foundCode)
/*this is where the code comes to, need to return it from here */
presentationMode.wrappedValue.dismiss()
}
var parent: QRCodeScan
init(_ parent: QRCodeScan) {
self.parent = parent
}
}
}
这是调用 qr 阅读器的 ContentView 的裁剪版本,这是我需要将找到的代码放回的地方
struct ContentView: View {
@State var presentQRScanner = false
var body: some View {
NavigationView{
Form{
Section(header: Text("Info")){
Button("Scan Barcode"){
self.presentQRScanner = true
}
.sheet(isPresented: $presentQRScanner){QRCodeScan()}
}
}
.navigationBarTitle(Text("New"), displayMode: .large)
.navigationBarItems(trailing: Button("Save"){
print("Button Pressed")
})
}
}
}
我在这里遇到了一个完全的障碍,我找不到任何资源可以让我从协调器传回数据,也许我实施了一些错误,但我似乎无法适应任何其他解决方案适合
非常感谢任何帮助。
谢谢
【问题讨论】: