|
I have a view that creates a list for each item in my data model. One of the properties in the data model is a note, which is a string that I've marked with @Published
This is the view that creates the list. The CycleDetailView allows the user to change the note. Which works correctly, but when I go back to the list, the CycleSummaryView has the old note, it doesn't get updated. When I quit the app and start again, it has the changed value. How do I get the CycleSummaryView to notice?
|
|
Bindings don't seem quite right here. I would expect updates in this case to be sent around with |
|
@TravelByRocket seems right. As you have a
|
|
That worked! I don't really understand when to use @Binding or @ObservedObject, etc. Is there a good reference which explains when to use them? |
|
I think there are a lot of explanations floating around out there and sometimes it's just a matter of finding the one the makes it click for you. I glanced at https://jaredsinclair.com/2020/05/07/swiftui-cheat-sheet.html and what stood out to me as a good shortcut/heuristic is that if it's |
|
Bryan gives good advice here:
This is one way that it clicked for me. When you're learning to program, most tutorials start with a Instead, get a piece of paper and draw 8 mock iPhone screens for some fake application. One is the sign-up screen, one is a list of something, another is a preferences tab, another is detail screen with a rating button, one is a theme picker, etc, etc. Get into the habit of thinking about your application as a collection of many views. (By the way, NONE of these views should be called Now when you look at each of these 8 mock screens (views) think how each of them have different states. The sign-up screen might have a button disabled because the user did not complete the email field. The theme picker might be in a state where the user is selecting colors based on a palette, or on a color wheel. In the preferences view, the user may be looking at the privacy tab, instead of the avatar tab. Each of these views have different states. So think about But what about bindings? @twostraws teaches a lesson where you create a subview (a ratings picker). You want that picker to change the value of an @State variable in a parent view. For example in your mock rating view, you have an |
|
The guys at objc.io developed a nifty flow chart for figuring out which property wrapper to choose for your state needs. It was published in their book Thinking in SwiftUI but you can see it reproduced here: SwiftUI Property Wrappers |
|
@roosterboy That flowchart is the most helpful and the ugliest I've ever seen! Nice find |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
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.