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
Try below code. (Please test in real device)
MasterView
DetailView1
DetailView2