TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to use #Preview

Forums > SwiftUI

I don't know how to fix this. I don't know how to code #preview part of EventView. Here is my code:

import SwiftUI

struct AddEvent: Codable, Identifiable {
    var id = UUID()
    let eventName: String
    let amount: Int
}

@Observable
class Events {
    var items = [AddEvent]() {
        didSet {
            if let encoded = try? JSONEncoder().encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }

    init() {
        if let savedItems = UserDefaults.standard.data(forKey: "Items") {
            if let decodedItems = try? JSONDecoder().decode([AddEvent].self, from: savedItems) {
                items = decodedItems
                return
            }
        }

        items = []
    }
}

let students = Bundle.main.decode("students.json")

struct ContentView: View {
    @State var events = Events()
    @State private var showingAddEvents = false

    var body: some View {
        NavigationStack {
            List {
                ForEach(events.items) { event in
                    NavigationLink {
                        EventView(event: event)
                    } label: {
                        HStack {
                            VStack {
                                Text(event.eventName)
                                Text("$ \(event.amount)")
                            }
                            Spacer()
                            Text("多少人沒繳錢")
                        }
                    }
                }
            }
            .navigationTitle("Payment Registration")
            .toolbar {
                Button("show add event", systemImage: "plus") {
                    showingAddEvents = true
                }
            }
            .sheet(isPresented: $showingAddEvents) {
                AddEventView(events: events)
            }
        }
    }
}

#Preview {
    ContentView()
}
import SwiftUI

struct EventView: View {
    let event: AddEvent

    var body: some View {
        let thisEventName = event.eventName

        NavigationStack {
            ForEach(students) {student in
                Text(student.name)
            }
            .navigationTitle(thisEventName)
        }
    }
}

#Preview {
    EventView()
}

1      

Add

#Preview {
    EventView()
      .environment(Events())
}

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.