SwiftUI - How to auto push 2th-DetailView when 1st-DetailView goback to MasterView using navigationView

189 views Asked by At

I'm newbie

my xcode version is 11.2.1 and swift is 5.1.2


When I use NavigationView in MasterView, and I have two DetailViews

I want to automatically enter the second DetailView when the first DetailView is closed


This is my approach, but it failed.

Whenever I enter the second DetailView,

it will be automatically back to the MasterView immediately

https://i.stack.imgur.com/9voJA.gif

and here is my code

struct MasterView: View {
    @State var tag: Int? = nil
    var body: some View {
        NavigationView{
            VStack{
                NavigationLink(destination: DetailView1(tag: self.$tag), tag: 1, selection: self.$tag) {
                    EmptyView()
                }
                NavigationLink(destination: DetailView2(tag: self.$tag), tag: 2, selection: self.$tag) {
                    EmptyView()
                }

                Button(action: {
                    self.tag = 1
                }) {
                    Text("press here to enter DetailView1")
                }
            }
        }

    }
}

struct DetailView1: View {
    @Binding var tag: Int?
    var body: some View {

        VStack{
            Text("DetailView 1")
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading:
            Button(action: {
                self.tag = 2
            }) {
                HStack {
                    Image(systemName: "arrow.left.circle")
                    Text("Go Back")
                }
        })
    }
}

struct DetailView2: View {
    @Binding var tag: Int?
    var body: some View {

        VStack{
            Text("DetailView 2 2 2 2 2 2 2")
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading:
            Button(action: {
                self.tag = nil  // never execute
            }) {
                HStack {
                    Image(systemName: "arrow.left.circle")
                    Text("Go Back")
                }
        })
    }
}

I found the "tag" variable will auto set "nil" when first DetailView is closed

but I don't know why

please help me, thank you

and sorry for my English is poor

1

There are 1 answers

1
Rohit Makwana On

Try below code. (Please test in real device)

MasterView

struct MasterView: View {
    @State var tag1: Int? = nil
    @State var tag2: Int? = nil
    var body: some View {
        NavigationView{
            VStack{
                NavigationLink(destination: DetailView1(tag: self.$tag2), tag: 1, selection: self.$tag1) {
                    EmptyView()
                }
                NavigationLink(destination: DetailView2(tag: self.$tag2), tag: 2, selection: self.$tag2) {
                    EmptyView()
                }
                Button(action: {
                    self.tag1 = 1
                }) {
                    Text("press here to enter DetailView1")
                }
            }
        }
    }
}

DetailView1

struct DetailView1: View {
    @Binding var tag: Int?
    @Environment(\.presentationMode) var presentation
    var body: some View {
        VStack{
            Text("DetailView -> 1")
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading:
            Button(action: {
                self.tag = 2
                self.presentation.wrappedValue.dismiss()
            }) {
            HStack {
                 Image(systemName: "arrow.left.circle")
                 Text("Go Back")
            }
        })
    }
}

DetailView2

struct DetailView2: View {
    @Binding var tag: Int?
    @Environment(\.presentationMode) var presentation
    var body: some View {
        VStack{
            Text("DetailView -> 2")
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading:
            Button(action: {
                self.tag = nil 
                self.presentation.wrappedValue.dismiss()
            }) {
            HStack {
                Image(systemName: "arrow.left.circle")
                Text("Go Back")
            }
        })
    }
}