Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

swift - NavigationView pops back to root, omitting intermediate view

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

Got this working, my issue now being that when I hit the Back button from ModelAddView, the intermediate view is omitted and it pops back to ContentView; a behaviour that ModelEditView does not have.

There's a reason for that I guess – how can I get back to ModelListView when dismissing ModelAddView?

Here's the code:

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...