Home > Mobile >  How to pass selected struct to another view?
How to pass selected struct to another view?

Time:07-11

I'm struggling to pass the right data to another View.
The idea is first to select the Task that has Status 0 and pass it to CardContentView to display under the Section of New tasks. If I print the values, its correct but it always displays the first data/array regardless of its Status. What could be done here?

struct Tasks: Identifiable {
            let id = UUID()
            let name: String
            let status: Int
            let image: String
        }
        extension Tasks {
        static var testData: [Tasks] {
            return [
                Tasks(name: "Inprogress", status: 1, image:"a1"),
                Tasks(name: "New", status: 0, image:"a2"),
            ]
        }
    }

ContentsView

struct ContentsView: View {
    @State var items: [Tasks]
    var size = 0
    
    var body: some View {
        NavigationView {
            List {
                let new = items.filter({$0.status == 0})
                let size = new.count
                if size > 0 {
                    Section(header:Text("\(size)"   " New")){
                        //let _ = print(new)
                        ForEach(new.indices, id: \.self) {itemIndex in
                            NavigationLink(destination: ChosenTask()) {
                                CardContentView(item: self.$items[itemIndex])

                            }
                        }
                    }
                }
            }
            .navigationBarTitle("My tasks")
        }

    }
}

CardContentView

struct CardContentView: View {
    @Binding var item: Tasks

    var body: some View {
        HStack {
            VStack(alignment: .leading,spacing: 5) {
                Label("Name: "   (item.name), systemImage: "person.crop.circle")
                    .font(.system(size: 12))
                    .labelStyle(.titleAndIcon)

            }
            .frame(maxWidth: .infinity, alignment: .leading)
            Image(item.image)
                .resizable()
                .frame(width: 60, height: 70)
        }
    }
}

CodePudding user response:

You are already passing the item to another view when you call CardContentView. You just have to do the same thing and pass the item to ChosenTask in your NavigationLink. When the user taps the item, SwiftUI will take care of creating and displaying the ChoseTask view for you.

You should also avoid using indices. There is no need. Your struct conforms to Identifiable so you can use it directly

var body: some View {
        NavigationView {
            List {
                let new = items.filter({$0.status == 0})
                if !new.isEmpty {
                    Section(header:Text("\(size)"   " New")){
                        //let _ = print(new)
                        ForEach(new) {item in
                            NavigationLink(destination: ChosenTask(item: item)) {
                                CardContentView(item: item)

                            }
                        }
                    }
                }
            }
            .navigationBarTitle("My tasks")
        }

    }
  • Related