I am trying to implement a NavigationView that follow a flow (rootView -> View1 -> View2 -> rootView)
I have implement the NavigationView inside the ContentView:
struct ContentView: View { @AppStorage("viewNavSelection") var viewNavSelection: String? var body: some View { NavigationView { VStack { NavigationLink(destination: Nav1View(), tag: NavViewSelectionEnum.view1.description, selection: self.$viewNavSelection) {EmptyView()} Button("Go to View1", action: { self.viewNavSelection = NavViewSelectionEnum.view1.description }) } } .navigationViewStyle(StackNavigationViewStyle()) } } Then each page use viewNavSelection to enable the NavigationLink with a Button
Each time you click on the button, the self.viewNavSelection will match the tag value and suppose to go to the next view (destination).
Issue is that after Going from viewRoot, to view1. Once I click on go to view3, it goes to the view and then right away bring me back to the root.
The viewNavSelection at that point is still set to view2.
I created a simple to reproduce the issue. Here's the code of each file:
struct Nav1View: View { @AppStorage("viewNavSelection") var viewNavSelection: String? var body: some View { VStack { Text("Nav1View") NavigationLink(destination: Nav2View(), tag: NavViewSelectionEnum.view2.description, selection: self.$viewNavSelection) {EmptyView()} Button("Go to View2", action: { self.viewNavSelection = NavViewSelectionEnum.view2.description }) } } } struct Nav2View: View { @AppStorage("viewNavSelection") var viewNavSelection: String? var body: some View { VStack { Text("Nav2View") NavigationLink(destination: Nav3View(), tag: NavViewSelectionEnum.view3.description, selection: self.$viewNavSelection) {EmptyView()} Button("Go to View3", action: { self.viewNavSelection = NavViewSelectionEnum.view3.description }) } } } struct Nav3View: View { @AppStorage("viewNavSelection") var viewNavSelection: String? var body: some View { VStack { Text("Nav3View") NavigationLink(destination: ContentView(), tag: NavViewSelectionEnum.viewRoot.description, selection: self.$viewNavSelection) {EmptyView()} Button("Pop to root", action: { self.viewNavSelection = NavViewSelectionEnum.viewRoot.description }) } } } Enum:
enum NavViewSelectionEnum : String { case view1 = "view1" case view2 = "view2" case view3 = "view3" case viewRoot = "viewRoot" init() { self = .viewRoot } var description: String { get { return self.rawValue } } } I am new to swiftUI, coming from Ionic/Angular background.. I try to find a playground so I could provide a link to the project. If it is easier I can upload it to git.
https://stackoverflow.com/questions/65557203/implement-navigationview-with-a-poptoroot-reset-every-two-views January 04, 2021 at 11:06AM
没有评论:
发表评论