简体   繁体   中英

SwiftUI - Nested NavigationLink

I have a layout that looks like this: Layout Drawing

There is a main view which is the Feed that would be my NavigationView and then I have views inside: PostList -> Post -> PostFooter and in the PostFooter A Button that would be my NavigationLink

struct Feed: View {
    var body: some View {
        NavigationView {
            PostList()
        }
    }
}

struct PostList: View {
    var body: some View {
        List {
            ForEach(....) {
                Post()
            }
        }
    }
}

struct Post: View {
    var body: some View {
        PostHeader()
        Image()
        PostFooter()
    }
}

struct PostFooter: View {
    var body: some View {
        NavigationLink(destination: Comment()) {
             Text("comments")
        }
    }
} 

But When when I tap on the comments, it goes to the Comment View then go back to Feed() then back to Comment() then back to Feed() and have weird behaviour.

Is there a better way to handle such a situation?

Update

The Navigation is now working but the all Post component is Tapeable instead of just the Text in the PostFooter. Is there any way to disable tap gesture on the cell and add multiple NavigationLink in a cell that go to different pages?

How about programmatically active the NavigationLink, for example:

struct PostFooter: View {
    @State var commentActive: Bool = false
    var body: some View {
        VStack{
            Button("Comments") {
                commentActive = true
            }
            NavigationLink(destination: Comment(), isActive: $commentActive) {
                EmptyView()
            }
        }
    }
} 

Another benefit of above is, your NavigationLink destination View can accept @ObservedObject or @Binding for comments editing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM