如何将从列表中选择的多个项目传递到下一个视图?

Posted

技术标签:

【中文标题】如何将从列表中选择的多个项目传递到下一个视图?【英文标题】:How do I pass multiple items selected from list to next view? 【发布时间】:2021-09-16 17:11:07 【问题描述】:

在我看来,我问了用户 2 个问题。在每个问题下方,我都有一个列表,允许用户选择多个项目。

    如何打印选择以在 Xcode 中调试控制台?

这是我在 Xcode 中的 Swift 代码:

import SwiftUI

struct WelcomeView: View 
    
    @State var applications: [Application] = [Application(name: "Beer"),
                                              Application(name: "Hard Seltzer")]
    
    @State var legislations: [Legislation] = [Legislation(name: "Natural"),
                                              Legislation(name: "Natural and Artificial")]
    
    var body: some View 
        NavigationView 
            VStack 
                Text("Which applications are you interested in?")
                    .padding()
                List
                    ForEach(0..<applications.count) index in
                        HStack 
                            Button(action: 
                                applications[index].isSelected = applications[index].isSelected ? false : true
                            ) 
                                HStack
                                    if applications[index].isSelected 
                                        Image(systemName: "checkmark.circle.fill")
                                            .foregroundColor(.green)
                                            .animation(.easeIn)
                                     else 
                                        Image(systemName: "circle")
                                            .foregroundColor(.primary)
                                            .animation(.easeOut)
                                    
                                    Text(applications[index].name)
                                
                            .buttonStyle(BorderlessButtonStyle())
                        
                    
                
                Text("Which legislations are you interested in?")
                    .padding()
                List
                    ForEach(0..<legislations.count) index in
                        HStack 
                            Button(action: 
                                legislations[index].isSelected = legislations[index].isSelected ? false : true
                            ) 
                                HStack
                                    if legislations[index].isSelected 
                                        Image(systemName: "checkmark.circle.fill")
                                            .foregroundColor(.green)
                                            .animation(.easeIn)
                                     else 
                                        Image(systemName: "circle")
                                            .foregroundColor(.primary)
                                            .animation(.easeOut)
                                    
                                    Text(legislations[index].name)
                                
                            .buttonStyle(BorderlessButtonStyle())
                        
                    
                
                NavigationLink(destination: FlavorSearchView()) 
                    Text("Next")
                        .padding()
                
            
        
    


struct Application: Identifiable 
        var id = UUID()
        var name: String
        var isSelected: Bool = false


struct Legislation: Identifiable 
        var id = UUID()
        var name: String
        var isSelected: Bool = false


struct WelcomeView_Previews: PreviewProvider 
    static var previews: some View 
        WelcomeView()
    

屏幕预览:

link to screen view image

【问题讨论】:

请提供足够的代码,以便其他人更好地理解或重现问题。 【参考方案1】:

您可以将数组作为参数传递给下一个视图,如下所示:

NavigationLink(destination: FlavorSearchView(applications: applications, legislations: legislations)) 

并在视图的构造函数中打印选择:

struct FlavorSearchView: View 
    
    init (applications: [Application], legislations: [Legislation])
        self.applications = applications
        self.legislations = legislations
        
        print(applications)
        print(legislations)
    
    
    let applications: [Application]
    let legislations: [Legislation]
    
    var body: some View
        VStack 
    

这种方法肯定会触发 SwiftUI 中自动弹出的已知错误。在这里讨论: https://developer.apple.com/forums/thread/677333

【讨论】:

以上是关于如何将从列表中选择的多个项目传递到下一个视图?的主要内容,如果未能解决你的问题,请参考以下文章

将数据发送到下一个视图 - SwiftUI

SwiftUI-将@State变量传递给多个视图麻烦

如何将项目列表从视图传递到控制器(ASP.NET MVC 4)

如何在 Swift 中通过 Segue 将数据传递到下一个视图?

我如何使用意图从列表视图转到下一个活动? [复制]

如何获取列表视图的所有项目及其位置并将它们意图用于另一个活动,并且可以转到下一个和后退位置