NavigationView弹出回到根,省略中间视图

时间:2020-09-07 07:44:53

标签: swift swiftui

在导航中,我希望能够从ContentView-> ModelListView-> ModelEditViewModelAddView进入。

解决了这个问题,现在的问题是,当我从Back按下ModelAddView按钮时,中间视图被忽略了,并弹出回到ContentView;一种行为 ModelEditView 没有没有。

我想有一个原因–解雇ModelListView时如何回到ModelAddView

这是代码:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List{
                NavigationLink(
                    destination: ModelListView(),
                    label: {
                        Text("1. Model")
                    })
                Text("2. Model")
                Text("3. Model")
            }
            .padding()
            .navigationTitle("Test App")
        }
    }
}

struct ModelListView: View {
    @State var modelViewModel = ModelViewModel()
    var body: some View {
        List(modelViewModel.modelValues.indices) { index in
            NavigationLink(
                destination: ModelEditView(model: $modelViewModel.modelValues[index]),
                label: {
                    Text(modelViewModel.modelValues[index].titel)
                })
        }
        .navigationBarTitleDisplayMode(.inline)
        .navigationBarItems(
            trailing:
                NavigationLink(
                    destination: ModelAddView(modelViewModel: $modelViewModel), label: {
                        Image(systemName: "plus")
                    })
        )
    }
}

struct ModelEditView: View {
    @Binding var model: Model
    var body: some View {
        TextField("Titel", text: $model.titel)
    }
}

struct ModelAddView: View {
    @Binding var modelViewModel: ModelViewModel
    @State var model = Model(id: UUID(), titel: "")
    
    var body: some View {
        TextField("Titel", text: $model.titel)
    }
}

struct ModelViewModel {
    var modelValues: [Model]
    
    init() {
        self.modelValues = [ //mock data
            Model(id: UUID(), titel: "Foo"),
            Model(id: UUID(), titel: "Bar"),
            Model(id: UUID(), titel: "Buzz")
        ]
    }
}

struct Model: Identifiable, Equatable {
    let id: UUID
    var titel: String
}

1 个答案:

答案 0 :(得分:2)

当前在NavigationLink中放置.navigationBarItems可能会引起一些问题。

可能的解决方案是将NavigationLink移动到视图主体,并仅在导航栏按钮中切换变量:

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")
        }
    }
}