|
I am trying to create my own picker from a SwiftData table of Images. I am using this to allow for selecting a subset of images (based on a type) and then allowing a user to pick one of the images. However the following code is not updating the selected Image...
I don't understand why I am getting a |
|
|
|
@ApApp is tap dancing around the
Sometimes students just want an answer to a coding question. But I think your problem is application structure. Some find my answers a bit long, and tedious, preferring the short "here's how to fix" type answers. For example, I think logic code does not belong in your view.
I think if you took another pass at restructuring your code, you'll find a simple solution. You tap a card which executes a method in the card struct (or class). This, in turn, causes any views containing the card to redraw itself, remove itself from the deck or tableau, or some other rule you may have. Separate Logic and ViewsTo illustrate this, I might recommend you watch the other Paul's SwiftUI lectures. Paul Hagerty has been taping his excellent CS193p course at Stanford University for several years. In lectures 4 thru 9 he develops a nice card game. His game is complete with a stand alone deck that has all the game rules, but doesn't know how to draw itself. The game view is all about drawing the state of a card (face up, face down, matched, etc) but doesn't know any of the game's rules. It's a terrific example of separating logic from views. You might also see how he displays cards in a view, captures the player's intentions with finger taps, and passes those intentions to the Keep Coding!Please return here and let us know how you solved this coding problem. See -> cs193p.sites.stanford.edu/2023 |
|
|
|
Well, still not there.. but the classes helped me clean up a bit of my code to improve computed properties and adding a few functions to my SwiftData classes. My challenge is that in my old version of this app, I could use a simple picker to pick images from the user's PhotoLibrary and save each image into the CoreData storage. If the same image was used twice, I'd have two copies of it in CoreData. As you can see that was a really bad design and took up a ton of space, when you send the same card to 10 people at Christmas or New Years, etc. So my goal was to create my own Card Gallery, which I have done with the GreetingCard object. The CardView is just a display View I use in multiple places to show an individual card. Here is what the picker looks like so far: |
|
So far it correctly changes the list of images at the bottom from the gallery, based on the eventType (I still can't get it to be flush with the Form up top). But if I select the image it doesn't do anything. I have tried changing the image to a Button in the picker, but I think the @Binding is not correct.
I am still not grocking the correct approach here... |
|
After reading Paul's post here - How to use MVVM to separate SwiftData from your views, I am still trying to understand how seperating to MVVM would improve my situtation. I know fundementally I should do this, but I would still have the issue that the value in the final view (in this case the Picker) is not allowed to bubble up to the NewCardView. And it I try to switch to having NewCardView use @State for the selectedGreetingCard, I am having issues with GreetingCardPickerView, which for some reason do not happen for the usage of @State for eventType. eventType is only being read by GreettingCardPickerView in order to filter the greetingCards Query. I want to know which greetingCard was selected, so it can be used by NewCardView to create the new Card instance, however, any change to selectedGreetingCard seems to be disallowed. Something is still not lining up. |
|
|
|
OK, not an ellegant solution, but I just figured it out (will redesign my view from a form back to the older style I had). I have changed the code in the "NewCardView" to use a navigationLink:
|
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
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.