WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 47: List of Activity Titles not showing in Quantify (Habit Tracker) App

Forums > 100 Days of SwiftUI

Hello,

The list on my ContentView doesn't show the added Activity in the list. It just remains blank. I tried to take the help of AI but it's still too dumb to solve the problem. I know that the Activity is being added but the data isn't being displayed properly.

Here is my ContentView:

struct ContentView: View {
    var activities = Activities()

    var body: some View {
        NavigationStack {
            List {
                ForEach(activities.activities, id: \.id) { activity in
                    Text("Item")
                }
            }
            .navigationTitle("Quantify")
            .toolbar {
                NavigationLink {
                        AddView(activities: activities)
                } label: {
                    Image(systemName: "plus")
                }
            }
        }
    }
}

Here is my AddView:

struct AddView: View {
    @State private var title = "Untitled activity"
    @State private var description = ""

    @Environment(\.dismiss) var dismiss

    var activities: Activities

    var body: some View {
        NavigationStack {
            Form {
                TextField("Enter description", text: $description)
            }
            .navigationTitle($title)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button("Save") {
                        let activity = Activity(title: title, description: description)
                        activities.activities.append(activity)
                        dismiss()
                    }
                }

                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") {
                        dismiss()
                    }
                }
            }
        }
    }
}

Here is my Activity struct:

struct Activity: Identifiable {
    var title: String
    var description: String
    var id = UUID()
}

Here is my Activities class:

class Activities {
    var activities = [Activity]()
}

   

Just add @Observable to you class like so. It will trigger the view to udpate once there is a change in activities property

@Observable
class Activities {
    var activities = [Activity]()
}

Kind of old way prior if not mistaken to iOS 17 you declare your activites in ContentView like so:

@StateObject var activities = Activities()

and class

class Activities: ObservableObject {
    @Published var activities = [Activity]()
}

   

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.