SwiftUI:如何在推送另一个视图之前获取列表中的选定行

SwiftUI: How can I get the selected row in a List before pushing another view

又一场 SwiftUI 斗争!

我有一个包含列表的视图。当用户点击一行时,我想先将所选项目保存在我的 VM 中,然后再推送另一个视图。 我能想到的解决该问题的唯一方法是首先保存选定的行并使用另一个按钮来推送下一个视图。好像不能一键搞定。

有人知道吗?

顺便附上代码:

struct AnotherView : View {
    @State var viewModel = AnotherViewModel()

    var body: some View {
        NavigationView {
            VStack {
                    List(viewModel.items.identified(by: \.id)) { item in
                        NavigationLink(destination: DestinationView()) {
                            Text(item)
                        }
                        // Before the new view is open, I want to save the selected item in my VM, which will write to a global store.
                        self.viewModel.selectedItem = item
                    }
                }
        }
    }
}

谢谢!

好的,我找到了一个不太阴暗的解决方案。 我用这篇文章https://ryanashcraft.me/swiftui-programmatic-navigation 向他喊出来! 我没有使用 NavigationLink 按钮,而是使用常规按钮,在用户点击时保存所选项目,然后使用 NavigationDestinationLink 按原样推送新视图 self.link.presented?.value = true

从 beta 3 开始就非常有效! 如果下一个测试版有什么变化,我会更新我的 post。

它可能是这样的:

struct AnotherView : View {
    private let link: NavigationDestinationLink<AnotherView2>
    @State var viewModel = AnotherViewModel()

    init() {
        self.link = NavigationDestinationLink(
            AnotherView2(),
            isDetail: true
        )
    }

    var body: some View {
        NavigationView {
            VStack {
                List(viewModel.items.identified(by: \.id)) { item in
                    Button(action: {
                        // Save the object into a global store to be used later on
                        self.viewModel.selectedItem = item
                        // Present new view
                        self.link.presented?.value = true
                    }) {
                        Text(value: item)
                    }
                }
            }
        }
    }
}

这也可以通过在任何 View 继承者中使用 Publisher 和 OnReceive 挂钩来实现。

您可以添加简单的点击手势

                NavigationLink(destination: ContentView() ) {
                    Text("Row")
                        .gesture(TapGesture()
                            .onEnded({ _ in
                                //your action here
                    }))
                }

Swift 5.1

如果您想将其应用于静态列表。你可以试试这个。

NavigationView{
        List{
            NavigationLink("YourView1Description", destination: YourView1())

            NavigationLink("YourView2Description", destination: YourView2())

           NavigationLink("YourView3Description", destination: YourView3())

           NavigationLink("YourView4Description", destination: YourView4())

        }
        .navigationBarTitle(Text("Details"))
    }