|
If you instantiate a class that implements ObservableObject and mark it as @StateObject in your parent view (eg. ContentView) and then pass it in to a child view through the initializer where it is marked with @ObservedObject, you can then read its properties in the child view. However, what if you want to update the object in the child view? If I take my object in the child view and try to update it like this:
Xcode won't complain, but it also doesn't work. Neither does this:
Oddly append works:
So how do we share an object across views with full read/write access everywhere? |
|
Does your class have any method to "save" the changes that you are trying to pass to the @Published var ? |
|
|
|
@Legion , is this what you are trying to achieve, change @Published from using @ObservedValue ?
|
|
|
|
@Legion , you have to post the code and tell where the issue is, i will give it a try |
|
Activity Struct:
Activities Class:
ContentView:
ActivityDetailView - This is where the problem is. As soon as I try to do anything other than append to activities.activityList this view crashes and returns to ContentView. You can see in updateTimesCompleted() a bunch of my different attempts which all fail.
AddActivityView - This works fine, but this is consistent with what I'm experiencing in ActivityDetailView as it only uses append and append is the only thing that works.
|
|
I'm copying this from another thread so the solution is present here also. Got some help in r/iosprogramming. I think the hints for the challenge are perhaps out of date and misleading based on this article which explained how to do it: https://www.swiftbysundell.com/articles/bindable-swiftui-list-elements/ You don't even have to pass the class holding the collection to the view or mess around with indices. Here's the solution:
|
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.