0

I have two views, ViewAssignment and TaskDetailView. My ViewAssignment page fetches data from an environment object, and creates a list using the data.

Upon each item of the list being clicked on, the TaskDetailView pops in as a navigation link, however, I am having trouble making the information in the TaskDetailView unique to that particular iteration (the item in the list)

I believe the trouble comes from my TaskDetailView.swift

import SwiftUI

struct TaskDetailView: View {
    @EnvironmentObject var assignment: Assignments
    @State var taskNotes = ""
    var body: some View {
        VStack(spacing: 10) {
            Image("english-essay")
                .resizable()
                .scaledToFit()
                .frame(width: 250, height: 160)
                .cornerRadius(20)
            
            
            Text(self.assignment.data.first?.taskName ?? "Untitled Task")
                .font(.title2)
                .fontWeight(.semibold)
                .lineLimit(2)
                .multilineTextAlignment(.center)
            
            
            
            
            HStack(spacing: 20) {
                Label(self.assignment.data.first?.weighting ?? "0", systemImage: "percent")
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                
                Text(self.assignment.data.first?.dueDate ?? "No Date")
                    .font(.subheadline)
                    .foregroundColor(.secondary)
            }
            
            TextField("Write any notes here", text: $taskNotes)
                .font(.body)
                .padding()
            
            Spacer()
            
        }
        
    }
}

struct TaskDetailView_Previews: PreviewProvider {
    static var previews: some View {
        TaskDetailView() // I assume there is some information I have to pass through here
    }
}

For details, this is my other view:

import SwiftUI

struct ViewAssignment: View {
    
    // Observed to update the UI
    @EnvironmentObject var assignment: Assignments
    
    var body: some View {
        ZStack {
            NavigationView {
                
                List(self.assignment.data) { task in
                    NavigationLink (
                        destination: TaskDetailView(),
                        label: {
                            Image(systemName: "doc.append.fill")
                                .scaleEffect(2.5)
                                .padding()
                            
                            VStack(alignment: .leading, spacing: 3) {
                                
                                Text(task.taskName)
                                    .fontWeight(.semibold)
                                    .lineLimit(2)
                                
                                Text(task.dueDate + " - " + task.subject)
                                    .font(.subheadline)
                                    .foregroundColor(.secondary)
                            }
                        })
                    
                }
                .navigationTitle("My Tasks")
                .listStyle(InsetGroupedListStyle())
                
                
            }
            
        }
    }
    
}








struct ViewAssignment_Previews: PreviewProvider {
    static var previews: some View {
        ViewAssignment()
    }
}

I would also like to know if, upon making the screen unique for each item in the list, would I be able to have the contents of the text field saved upon reloading the app, Perhaps through @AppStorage? Thank you for the assistance.

fesari
  • 33
  • 7

1 Answers1

0

If I understand correctly what you are trying to do: a TaskDetailView displays the detail of a ... Task. So you should have a Task structure like this:

struct Task {
    let name: String
    let subject: String
    ...

}

You have to create one (or more) instance of Task to test your TaskDetailView:

extension Task {
    var test: Task {
        Task(name: "Test", subject: "Test Subject")
    }
}

Now in the preview of your TaskDetailView you can try to display your example :

struct TaskDetailView_Previews: PreviewProvider {
    static var previews: some View {
        TaskDetailView(task: Task.test) // here
    }
}

For the moment nothing is happening. Because your TaskDetailView doesn't have a task parameter.

struct TaskDetailView: View {
    
    var task: Task
    
    var body: some View {
        ...
    }

Now its body can use the different parameters of this Task.

    Text(task.name)
                .font(.title2)
                .fontWeight(.semibold)
                .lineLimit(2)
                .multilineTextAlignment(.center)

Now in your List:

List(self.assignment.data) { task in
    NavigationLink (
        destination: TaskDetailView(task: task),    // <-  here !!!
        label: {
            Image(systemName: "doc.append.fill")
                .scaleEffect(2.5)
                .padding()
            
            VStack(alignment: .leading, spacing: 3) {
                
                Text(task.name)
                    .fontWeight(.semibold)
                    .lineLimit(2)
                
            }
        })
    
}
Adrien
  • 1,579
  • 6
  • 25