|
I have a SwiftData based project where I am trying to convert from using a custom Color picker to the SwiftUI ColorPicker. I already store colors in my model as Text. I am trying to migrate to using I'm using the techniques documented in the following article. This works fine using the following patterns. No problems with this. blog.eidinger.info/from-hex-to-color-and-back-in-swiftui
The problem I'm having is in my update view. I'm using bound properties to support the updates. I don't know how to handle color. The property needs to be bindable to the color property in my model ( I feel I need to initialize
The following code doesn't compile, of course.
edit... I realize it may be useful to see the 2 views where I invoke
|
|
hi Jay, you might check out this recent Stewart Lynch video, where he builds out a SwiftData model having a hope that's of interest, DMG |
|
thanks for the quick response. I may need to review his content in more detail, but I don't think his project addresses my problem. He does utilize the hex color conversion for his genre property. However, he does not have any logic relating to editing the genre color once set. I have no problem saving an initial Category.color with the correct hex value and then coverting it back for subsequent use in other views. I can't figure out how to create my edit view to allow edits to that property. He doesn't seem to handle that use case. |
|
hi Jay, if you keep watching, you'll see the NewGenresView in which you define a new Genre. this view uses a SwiftUI color picker linked to the view's although this view only adds a new Genre, you can use the same code to accept an incoming Genre and, either using an init() or an .onAppear modifier, set up the state variable to have a starting value of the incoming genre's color property. when the view is dismissed, just copy the value of the state variable (a Color) over to the Genre. if you'd like something of a similar flavor, then take a look at my shopping list project, where each hope that helps, DMG |
|
Thanks again for this help. Between review of this code and some online assistance with Phind.com I was able to fix the views. In brief, I refactored both the Add and CategoryUpdate views to move to a single state variable for category. The code I needed within CategoryUpdateView to enable the ColorPicker and my hex stored values is the following:
Everything is working as expected now. Thanks again for the tips. I always enjoy learning from other's solutions! cheers -- jay |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.