NavigationView pops back to root, omitting intermediate view

后端 未结 1 1164
天涯浪人
天涯浪人 2020-12-22 02:17

In my navigation, I want to be able to go from ContentView -> ModelListView -> ModelEditView OR ModelAddView.

<
相关标签:
1条回答
  • 2020-12-22 02:29

    Currently placing a NavigationLink in the .navigationBarItems may cause some issues.

    A possible solution is to move the NavigationLink to the view body and only toggle a variable in the navigation bar button:

    struct ModelListView: View {
        @State var modelViewModel = ModelViewModel()
        @State var isAddLinkActive = false // add a `@State` variable
    
        var body: some View {
            List(modelViewModel.modelValues.indices) { index in
                NavigationLink(
                    destination: ModelEditView(model: $modelViewModel.modelValues[index]),
                    label: {
                        Text(modelViewModel.modelValues[index].titel)
                    }
                )
            }
            .background( // move the `NavigationLink` to the `body`
                NavigationLink(destination: ModelAddView(modelViewModel: $modelViewModel), isActive: $isAddLinkActive) {
                    EmptyView()
                }
                .hidden()
            )
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarItems(trailing: trailingButton)
        }
    
        // use a Button to activate the `NavigationLink`
        var trailingButton: some View {
            Button(action: {
                self.isAddLinkActive = true
            }) {
                Image(systemName: "plus")
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题