SwiftUI 以编程方式从可表示返回到视图 [英] SwiftUI go back programmatically from representable to View
问题描述
我正在尝试在新的 swift ui 应用中设置 qr 阅读器.
I'm trying to setup a qr reader within a new swift ui app.
我可以用这一行加载 UIKit qr 阅读器视图
I can get load the UIKit qr reader view with this line
NavigationLink(destination: QRCodeScan()){Text("Scan QR")}
这是我的 ViewControllerRepresentable
This is my 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
}
}
}
在返回最后一页..."这一行,我需要以编程方式返回到将用户发送到 qr 扫描仪的页面.该页面加载了一个导航后退按钮,我几乎需要复制此按钮行为以在需要时调用
At the line 'Go back to the last page...' I need to programmatically return to the page which sent the user to the qr scanner. The page loads with a navigation back button, I pretty much need to replicate this buttons behaviour to call when I need
感谢任何帮助/指示
tia
推荐答案
简短的回答是你现在不能这样做.既没有绑定也没有环境值可以触发这个设置.我的猜测是会有某种类似于 presentationMode
的环境值,您可以利用它,但目前还没有公布.
The short answer is you can't do that right now. There is neither a binding nor an environment value to set that can trigger this. My guess is there will be some kind of environment value akin to presentationMode
that you can tap into but it isn't currently advertised.
您可以尝试当前的 presentationMode
,但我真正的建议是将您的 QR 扫描仪呈现为表格而不是推送.无论如何,从导航的角度来看,这实际上可能更有意义.要做到这一点,在你的演示者中设置一个 @State
变量来处理它何时被呈现.
You could try the current presentationMode
but my real suggestion is to present your QR scanner as a sheet rather than a push. This may actually make more sense from a navigational standpoint anyway. To do it this way, in your presenter set up a @State
var to handle when it's presented.
@State var presentQRScanner = false
var body: some View {
Button("Scan") {
self.presentQRScanner = true
}
.sheet(isPresented: $presentQRScanner) { QRCodeScan() }
}
然后,当您想以编程方式关闭时,您的 UIViewControllerRepresentable
:
Then, when you want to programmatically dismiss, your UIViewControllerRepresentable
:
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
func scannedCode() {
presentationMode.wrappedValue.dismiss()
}
或者,您也可以通过在使用代码调用的 QRCodeScan 上创建一个闭包来从演示者那里推动这一点.
Alternatively, you can drive this from the presenter too by creating a closure on the QRCodeScan that gets invoked with the code and you have your presenter dismiss.
var onCodeScanned: (Code) -> Void = { _ in }
func scannedCode() {
onCodeScanned(code)
}
在演示者中:
var body: some View {
Button("Scan") {
self.presentQRScanner = true
}
.sheet(isPresented: $presentQRScanner) {
QRCodeScan(onCodeScanned: {
self.process($0)
self.presentQRScanner = false
})
}
}
不知道 isActive
绑定,如果您仍然想将视图推送到导航堆栈而不是呈现它,那实际上应该对您有用.
was not aware of the isActive
binding, that should actually work for you if you still want to push your view on the nav stack instead of present it.
这篇关于SwiftUI 以编程方式从可表示返回到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!