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

How to update a data from another view in swiftUI?

Forums > SwiftUI

Hi, currently I want to update a class from my DayView from another view DashBoard. I tried to pass the ListOfDays object into the Dashboard view and attempted to update the StickCount using the "Smoke" button, but the change wasn't seen when i run the app. I would appreciate any advice given, thanks!

DayView.swift

class ListOfDays: ObservableObject {
    struct SingleDay: Identifiable {
        var id: UUID
        var dayNumber: Int
        var stickCount: Int
        var moneySaved: Float
        var date: Date

        init(dayNumber: Int, stickCount: Int, moneySaved: Float, date: Date) {
            self.id = UUID()
            self.dayNumber = dayNumber
            self.stickCount = stickCount
            self.moneySaved = moneySaved
            self.date = Date()
        }
    }

    @Published var course: [SingleDay] = Array(1...28).map { SingleDay(dayNumber: $0, stickCount: 0, moneySaved: 0.00, date: Date())}
}

struct DayView: View {
    @ObservedObject var daysList = ListOfDays()

    var body: some View {
        NavigationView {
            List {
                ForEach(0 ..< daysList.course.count, id: \.self) { index in
                    NavigationLink(destination: DetailedDayView(daysList: self.daysList, index: index)) {
                        HStack{
                            Text("\(self.daysList.course[index].dayNumber)")
                        }
                    }
                }
            }
            .navigationBarTitle(Text("Days"))
        }
    }
}

struct DayView_Previews: PreviewProvider {
    static var previews: some View {
        DayView()
    }
}

Dashboard.swift

struct Dashboard: View {
    @StateObject var daysList = ListOfDays()

    var body: some View {
        VStack {
                VStack {
                    HStack {
                        HelloName()
                        Spacer()

                        DayNumber()
                    }
                    .frame(height: 60)

                    // Progress Chart
                    ProgressText()

                    Spacer()
                    Button("Smoke") {
                        var count = self.daysList.course[2].stickCount
                        count += 1
                        self.daysList.course[2].stickCount = count
                    }
                }
            }
        }
    }

The HelloName() and DayNumber() and ProgressText() are just subviews

2      

You're not actually passing anything between these two Views. What you are doing is creating a ListOfDays object in DayView:

@ObservedObject var daysList = ListOfDays()

and then creating a new, completely separate ListOfDays object in Dashboard:

@StateObject var daysList = ListOfDays()

So updating either one of those will have no effect on the other.

If I'm correctly understanding what you are trying to accomplish, then you should be doing something like this instead:

struct DayView: View {
    @StateObject var daysList = ListOfDays()

    ...
}

struct Dashboard: View {
    @ObservedObject var daysList: ListOfDays

    //although you could just use the memberwise initializer
    //we'll make it explicit here
    init(daysList: ListOfDays) { 
        self.daysList = daysList
    }

    ...
}

Then, somewhere in DayView (like in a NavigationLink or whatever), you would need:

Dashboard(daysList: daysList)

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.