Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass selected struct to another view?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading