Hello,
I use Navigationstack in the first and second views. In this case, the second view is executed immediately and then disappears. Is there a solution? The code is as follows.
device = i-phone
struct ContentView: View { @State var path: [String] = []
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("Screen1")
Spacer()
Button(action: {
path.append("2nd")
}, label: {
Text("go to 2nd Screen")
})
}
.navigationDestination(for: String.self) { s in
if s == "2nd" {
let _ = print("1st screen nav path(1) = \(path)")
SecondScreen(path: $path)
let _ = print("1st screen nav path(2) = \(path)")
}
}
.onAppear {
print("1st screen on appear")
print("1st screen path = \(path)")
}
.onDisappear {
print("1st screen on disappear")
print("1st screen disa. path = \(path)")
}
.padding()
}
}
}
struct SecondScreen: View {
@State var path2: [String] = []
@Binding var path: [String]
var body: some View {
NavigationStack(path: $path2) {
VStack {
Text("2nd Screen is loaded")
Spacer()
Button(action: {
path2.append("3rd")
}
, label: {
Text("go to 3rd Screen")
})
Button(action: {
path2.removeLast()
}
, label: {
Text("back to 1st Screen")
})
}
.navigationDestination(for: String.self) { s in
if s == "3rd" {
// Text("3rd")
thirdScreen()
} else {
thirdScreen()
}
}
}
.onAppear {
print("2nd screen on appear")
print("2nd screen path = \(path)")
print("2nd screen path2 = \(path2)")
}
.onDisappear {
print("2nd screen on disappear")
print("2nd screen path = \(path)")
print("2nd screen disa. path2 = \(path2)")
}
}
}
struct thirdScreen: View { var body: some View { VStack { Text("3rd Screen") } .onAppear { print("3rd screen on appear") } .onDisappear { print("3rd screen on disappear") } } }