繁体   English   中英

SwiftUI - 嵌套导航链接

[英]SwiftUI - Nested NavigationLink

我有一个看起来像这样的布局:布局图

有一个主视图,它是我的 NavigationView 的提要,然后我在里面有视图: PostList -> Post -> PostFooter 和 PostFooter 一个按钮,这将是我的 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")
        }
    }
} 

但是当我点击评论时,它会进入评论视图,然后 go 返回 Feed() 然后返回 Comment() 然后返回 Feed() 并出现奇怪的行为。

有没有更好的方法来处理这种情况?

更新

Navigation 现在可以工作,但所有 Post 组件都是 Tapeable,而不仅仅是 PostFooter 中的 Text。 有什么方法可以禁用单元格上的点击手势并在 go 到不同页面的单元格中添加多个 NavigationLink?

如何以编程方式激活 NavigationLink,例如:

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

上面的另一个好处是,您的 NavigationLink 目标视图可以接受@ObservedObject@Binding进行评论编辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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