Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

swiftui - Back button in UIViewControllerRepresentable goes to root view instead of going back to previous view

I have a SwiftUI app that has three levels:

  • Main List (SwiftUI NavigationView)
  • Secondary List (SwiftUI NavigationView)
  • An UIViewControllerRepresentable

In my secondary list I have

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

And then in the UIViewControllerRepresentable I have

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>) {
    
}
}

The back button that appears should go back to the Secondary List, but instead it goes back to the Main List.

question from:https://stackoverflow.com/questions/66065702/back-button-in-uiviewcontrollerrepresentable-goes-to-root-view-instead-of-going

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved my problem! The NavigationLink to my UIViewControllerRepresentable was in the NavigationBar and that messed up with the hierarchy.

I moved the NavigationLink outside of the NavigationBar and now is working as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...