问题
Notice in the gif that once I navigate and dismiss the new view, I am unable to navigate back! Is this a SwiftUI bug or a misuse of NavigationLinks?
struct ContentView: View {
var body: some View {
return NavigationView {
NavigationLink(destination: FakeView1()) {
Text("Navigate")
}
}
}
}
struct FakeView1: View {
var body: some View {
Text("Hey")
}
}

回答1:
This seems swiftUI bug. I also faced the same issue so, I have used this workaround for it.
struct ContentView: View {
@State var isFakeActive: Bool = false
var body: some View {
NavigationView {
NavigationLink(destination: FakeView1(isFakeActive: self.$isFakeActive), isActive: self.$isFakeActive) {
Text("Navigate")
}
}
}
}
And for your FakeView1 class.
struct FakeView1: View {
@Binding var isFakeActive: Bool
var body: some View {
Text("Hey")
.navigationBarItems(leading: Button(action: {
self.isFakeActive = false
}, label: {
HStack {
Image(systemName: "arrow.left")
Text("Back")
}
}))
}
}
I have tested and it is working fine.
来源:https://stackoverflow.com/questions/59483962/swiftui-unable-to-navigate-back-and-forth-with-navigationlink