-1

I am working on a project that is attempting to present and dismiss views in a NavigationView using state and binding. The reason I am doing this is there is a bug in the @Environment(.presentationMode) var presentaionMode: Binding model. It's causing odd behavior. It's discussed in this post here.

The example below has three views that are progressively loaded on to the view. The first two ContentView to NavView1 present and dismiss perfectly. However, once NavView2 is loaded, the button that is used to toggle the state of presentNavView2 ends up adding another NavView2 view on the stack and does not dismiss it as expected. Any thoughts as to why this would be?

ContentView

struct ContentView: View {
    @State private var presentNavView1 = false
    
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: NavView1(presentNavView1: self.$presentNavView1), isActive: self.$presentNavView1, label: {
                    Button(action: {
                        self.presentNavView1.toggle()
                    }, label: {
                        Text("To NavView1")
                    }) // Button
                }) // NavigationLink
            } // List
            .navigationTitle("Home")
        } // NavigationView
    } // View
}

NavView1

struct NavView1: View {
    
    @State private var presentNavView2 = false
    
    @Binding var presentNavView1: Bool
    
    var body: some View {
        List {
            NavigationLink(destination: NavView2(presentNavView2: self.$presentNavView2), isActive: self.$presentNavView2, label: {
                Button(action: {
                    self.presentNavView2.toggle()
                }, label: {
                    Text("To NavView2")
                }) // Button
            }) // NavigationLink
            Button(action: {
                self.presentNavView1.toggle()
            }, label: {
                Text("Back")
            })
        } // List
        .navigationTitle("NavView1")
    } // View
}

NavView2

struct NavView2: View {
    @Binding var presentNavView2: Bool

    
    var body: some View {
        VStack {
            Text("NavView2")
            Button(action: {
                self.presentNavView2.toggle()
            }, label: {
                Text("Back")
            }) // Button
        } // VStack
        .navigationTitle("NavView2")
    }
}
jonthornham
  • 2,881
  • 4
  • 19
  • 35
  • I read your other question. I understand your decision to use the `Binding` vis a vis the UIViewControllerRepresentable, can you not use `@Environment(\.presentationMode) var presentationMode` in the rest of the app? – Yrb Nov 23 '21 at 21:35
  • It's part of the same view. It progresses beyond the view that uses the UIViewControllerRepresentable so I wanted to use the same pattern. \ – jonthornham Nov 23 '21 at 23:57
  • I think you may have a simpler time just dealing with the `UIViewControllerRepresentable` on its own, and using `@Environment(\.presentationMode) var presentationMode` for the rest. Did you file a radar on it? If so, what is the number? – Yrb Nov 24 '21 at 00:03
  • I did. Radar number is FB9779774. – jonthornham Nov 24 '21 at 22:57

1 Answers1

0

You can use DismissAction, because PresentationMode will be deprecated. I tried the code and it works perfectly! Here you go!

import SwiftUI

struct MContentView: View {
    @State private var presentNavView1 = false
    
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: NavView1(), isActive: self.$presentNavView1, label: {
                    Button(action: {
                        self.presentNavView1.toggle()
                    }, label: {
                        Text("To NavView1")
                    })
                })
            }
            .navigationTitle("Home")
        }
    }
}

struct NavView1: View {
    @Environment(\.dismiss) private var dismissAction: DismissAction
    
    @State private var presentNavView2 = false
    
    var body: some View {
        List {
            NavigationLink(destination: NavView2(), isActive: self.$presentNavView2, label: {
                Button(action: {
                    self.presentNavView2.toggle()
                }, label: {
                    Text("To NavView2")
                })
            })
            Button(action: {
                self.dismissAction.callAsFunction()
            }, label: {
                Text("Back")
            })
        }
        .navigationTitle("NavView1")
    }
}

struct NavView2: View {
    @Environment(\.dismiss) private var dismissAction: DismissAction

    var body: some View {
        VStack {
            Text("NavView2")
            Button(action: {
                self.dismissAction.callAsFunction()
            }, label: {
                Text("Back")
            })
        }
        .navigationTitle("NavView2")
    }
}

struct MContentView_Previews: PreviewProvider {
    static var previews: some View {
        MContentView()
    }
}