SwiftUI unable to navigate back and forth with navigationLink

坚强是说给别人听的谎言 提交于 2020-01-16 10:31:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!