gpt4 book ai didi

SwiftUI 以编程方式从可表示对象返回到 View

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

我正在尝试在新的 swift ui 应用程序中设置二维码阅读器。

我可以用这一行加载 UIKit 二维码阅读器 View

NavigationLink(destination: QRCodeScan()){Text("扫描二维码")}

这是我的 ViewControllerRepresentable

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 {
func codeDidFind(_ code: String) {
print(code)
//Go back to the last page, take 'code' with you
}

var parent: QRCodeScan

init(_ parent: QRCodeScan) {
self.parent = parent
}
}

在“返回最后一页...”这一行,我需要以编程方式返回到将用户发送到二维码扫描仪的页面。该页面加载了一个导航后退按钮,我非常需要复制此按钮的行为以在需要时调用

感谢任何帮助/指点

蒂亚

最佳答案

struct ContentView: View {
@State var isActive = false
@State var code = ""
var body: some View {
NavigationView {
ZStack {
NavigationLink(destination: DetailView(isActive: $isActive, code: $code), isActive: $isActive, label: { EmptyView() })
Button(action: {
self.isActive.toggle()
}, label: {
Text("navigate")
})
}
}
}
}
struct DetailView: View {

@Binding var isActive: Bool
@Binding var code: String

var body: some View {
Button(action: {
self.code = "new code"
self.isActive.toggle()
}) {
Text("Back")
}
}
}

这可能对你有帮助,使用 NavigationLink 的 isActive 参数来回导航

关于SwiftUI 以编程方式从可表示对象返回到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419335/

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