简体   繁体   English

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

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

Another SwiftUI struggle!另一个 SwiftUI 斗争!

I have a view that contains a list.我有一个包含列表的视图。 When user taps on a row, I want to first save the selected item in my VM then push another view.当用户点击一行时,我想先将所选项目保存在我的 VM 中,然后再推送另一个视图。 The only way I can think of to solve that issue is to first save the selected row and have another button to push the next view.我能想到的解决该问题的唯一方法是首先保存所选行并使用另一个按钮来推送下一个视图。 It seems impossible to do this with only one tap.只需轻轻一按似乎不可能做到这一点。

Anyone have a clue?有人有线索吗?

Here's the code by the way:顺便说一下,这是代码:

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

Thank you!谢谢!

Alright, I found a not too shady solution.好吧,我找到了一个不太阴暗的解决方案。 I used this article https://ryanashcraft.me/swiftui-programmatic-navigation shout out to him!我用这篇文章https://ryanashcraft.me/swiftui-programmatic-navigation向他大喊大叫! Instead of using a NavigationLink button, I use a regular button, save the selected item when the user tap then use NavigationDestinationLink to push the new view as is self.link.presented?.value = true .我没有使用NavigationLink按钮,而是使用常规按钮,在用户点击时保存所选项目,然后使用NavigationDestinationLinkself.link.presented?.value = true推送新视图。

Works like a charm as of beta 3!从 Beta 3 开始,它就像一个魅力! I'll update my post if something change in the next betas.如果下一个测试版有什么变化,我会更新我的帖子。

Here's how it could look like:这是它的样子:

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

You can add simple TapGesture您可以添加简单的 TapGesture

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

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

Swift 5.1斯威夫特 5.1

In case you would like to apply it to a static List.如果您想将其应用于静态列表。 You may try this.你可以试试这个。

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

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

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

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

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

暂无
暂无

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

相关问题 SwiftUI:如何在动态列表或 LazyVStack 中获取视图框架 - SwiftUI: How can I get the frame of a view in a dynamic List or LazyVStack 在 SwiftUI 中,如何使用另一个视图剪辑视图的一部分? - In SwiftUI how can I clip a part of a view with another view? 如何仅显示 SwiftUI 列表中的选定项目? - How can I show only selected items in SwiftUI List? 如何使用 for 循环获取 UNNotificationRequest 数组以填充 SwiftUI 列表视图? - How can I get UNNotificationRequest array using for loop to populate SwiftUI List view? 如何在 SwiftUI 中保留视图结构列表? - How can I keep a list of View structs in SwiftUI? 推送到另一个视图后,如何使此ios按钮在提要视图中保持可见? - How do i get this ios button to stay visible in the feed view after pushing to another view? iOS 开发:如何在将视图控制器推送到导航堆栈之前预加载它? - iOS Development: How can I preload a view controller before pushing it onto the navigation stack? 如何通过点击 SwiftUI 列表中的行元素从子视图返回到父视图? - How do I return from a child view to a parent view by tapping a row element in a list in SwiftUI? 如何在表格视图中获取选定行的文本? (迅速) - How do I get the text of a selected row in a table view? (Swift) 如何仅隐藏第一个和最后一个项目的 SwiftUI 列表视图的列表行分隔符? - How do I hide the list row separator of a SwiftUI List view for only the first and last items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM