如何以编程方式从SwiftUI导航堆栈中弹出UIViewControllerRepresentable

下面是我的SwiftUI设置:

我有一个SwiftUI视图(称为MapSearchView),它有一个包含NavigationLink的NavigationView。

    var body: some View {
    VStack {
        NavigationView {
            Form {
                Section (header: Text("Location")){
                    NavigationLink (locationString, destination: GooglePlacesViewController(mapTools: $mapTools))
                }...

NavigationLink的目标是包装GooglePlacesViewController的UIViewControllerRepresentable。当我完成我的GooglePlaces搜索(因为我做了选择或者我取消了搜索),我想以编程方式弹回MapSearchView。

我尝试在UIViewControllerRepresentable (GooglePlacesViewController)中添加以下内容:

    @Environment(\.presentationMode) var presentationMode

var popToPrevious : Bool = false {
    didSet {
        if popToPrevious == true {
            self.presentationMode.wrappedValue.dismiss()
        }
    }
}

并使用以下命令在我的协调器中将popToPrevious设置为真

parent.popToPrevious = true

它确实会被调用,但什么也不做。

我也试着让协调员给我打电话

view.viewController().navigationController.popViewController(animated: true)

( viewController ()是一个获取任何视图的viewController的扩展)

这也不做任何事情。

想法?

转载请注明出处:http://www.bizarre-animals.com/article/20230401/1241564.html