|
Hi all, I'm adding validation to the Bookwork genre picker. (I'm using a similar approach to the Order validation in Cupcake Corner.) It's very simple, just checking for non-empty:
The problem is that the Picker indicates that Fantasy is selected even when I haven't touched it. If I open the Picker, it shows a checkmark next to Fantasy! But the binding back to the genre string is empty! It will only get set properly if I explicitly choose Fantasy or some other genre in the list. How do I make the Picker reflect the fact that no value has been picked yet? The default behavior is super deceptive, if I were a user I would assume that Fantasy was selected by default and that I wouldn't have to do anything if I wanted to enter another Fantasy novel. (Edit - sorry for the ginormous screenshot, I don't know how to tell this text editor to scale it down. Other picker styles, like .inline and .segmented do not have this deceptive behavior. But they also do not match the style that I actually want, which is the default menu-like behavior. |
|
Geoff wonders about
Please don't show ALL your view code. But can you paste in the snips showing your "Geoff" is the "G" hard like goose? or soft like Jeff? |
|
Hi Obelix, it's pronounced identically to "Jeff" :) Variable declaration:
List of genres:
Picker code:
The code is identical to Paul's, having followed along from his tutorials. (I also opened up his project-11 from github to confirm the behavior is the same.) The differences are in the validation which is elsewhere in the code. My question is how to tell Picker to not deceptively show "Fantasy" by default when it has not actually been picked. |
|
Yikes! I thought there'd be an easy-to-find typo. Now I'm not sure, leading me to think about default See -> Picker Behavior |
|
Thanks Obelix, I realize now that the problem I have is with iOS 16 default Picker behavior. In the video, https://youtu.be/fC3WxO6Z2T4?t=749, Paul is running 15.x and there is no confusion. However in iOS 16 the new default style is like .menu which causes confusion at least for me. I just verified myself by installed the iOS 15 simulator (which got deleted for me when upgrading to XCode 14). I have thought about creating an invalid value for the Picker to render and to handle it later during the validation step. This would work, but it is in my opinion is just a hack.
I hope Apple provides an update to Picker for iOS 16 which brings back the more intuitive default behavior in iOS 15.x. |
|
I published my app the same day that Apple launched iOS16, and all my pickers were broken, just for the reason geoff says. There was no documentation at all in betas for that change, and I had to reconstruct it quickly. Bada, very bad, Apple. I'm still trying to get the options I had with this new "menu" style, the most important being in fact default values, or adding new options to this pickers just from the picker itself. I'm trying this way:
Any further ideas? |
|
I have just run into this issue and luckily found this thread. My workaround was to set the var for genre to the value equal to the first value in genres. @State private var genre = "Fantasy" let genres = ["Fantasy", "Horror", "Kids", "Mystery", "Poetry", "Romance", "Thriller"] now genre is never empty and always equals the first item unless changed. What I could not do though was set genre to genre[0], kept getting an error about "Cannot use instance member 'genres' within property initializer; property initializers run before 'self' is available" Any help appreciated |
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.