NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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

   

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)

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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.