UIViewControllerRepresentable 中的后退按钮转到根视图而不是返回上一个视图

时间:2021-02-05 15:08:58

标签: swiftui swiftui-navigationlink uiviewcontrollerrepresentable

我有一个包含三个级别的 SwiftUI 应用:

  • 主列表(SwiftUI NavigationView)
  • 二级列表(SwiftUI NavigationView)
  • 一个 UIViewControllerRepresentable

在我的二级列表中

NavigationLink(destination: ARView(museum: self.museum)) {
    Image(systemName: "camera.viewfinder")
})

然后在 UIViewControllerRepresentable 我有

struct ARView: UIViewControllerRepresentable {

var museum:Museum?
var work:Work?

func makeUIViewController(context: UIViewControllerRepresentableContext<ARView>) -> UIViewController {
    let ARVC = ARViewController(nibName: "ARViewController", bundle: .main)
    if let museum = museum {
        ARVC.visitedMuseum = museum
        ARVC.ARExperience = false
        LoggingSystem.push(eventLog: ["event":"AR opened","museum":ARVC.visitedMuseum.name], verbose: false)
    }
    if let work = work {
        ARVC.visitedWork = work
        ARVC.VRonly = true
        LoggingSystem.push(eventLog: ["event":"Work selected","work":ARVC.visitedWork.name], verbose: false)
    }
    return ARVC
}

func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<ARView>) {
    
}
}

出现的后退按钮应该返回到二级列表,但它返回到主列表。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题! 到我的 UIViewControllerRepresentable 的 NavigationLink 位于 NavigationBar 中,这与层次结构混乱。

我将 NavigationLink 移到了 NavigationBar 之外,现在按预期工作。