|
Hello, I have 2 views. A Main view and a Settings view. On the Settings view, I can select working hours with a picker. The value will be saved in an AppStorage value after I click the save button. That works. Now, when I call the settings view again, I want that the saved value for work hours will be preselected. At the moment there is just the default value selected. I've tried to assign it like this:
But I get the error: Cannot use instance member within property initializer. Here is my full code of both views:
I really appreciate any help how to fix that. Thanks in advance Kay |
|
Get rid of this:
You can't have bare statements like that at the root level of your struct. Instead, you can do this:
|
|
Take a breath and think about your problem. To me, it seems you are struggling with two concepts. Take a big problem and break it down into smaller, solvable problems. There's another issue with naming, but that's minor. First, let's look at passing data from one view to another view. Because views are structs, you know you get a COPY of values that you pass in. But you want two views to SHARE the same value. This is solved using bindings. @twostraws has some great video on bindings. Please watch them and take notes. Then watch them again while reviewing your notes. In your case, you have one view where you want to store the index to an array of time values. You are offering the user a choice between 35, 38.5, and 40. However you are storing the index to this choice, 0, 1, or 2. Recommend you change your variable to timeIndex (camelCase!). So focus first on this feature. A binding allows you to pass a value from one view to a second view. You can change the value in the second view and it is automatically updated in the first view. The second feature is a requirement to save this index using AppStorage. You are using the AppStorage property wrapper. Nice! Review this article for more info on AppStorage. In short, whenever you change the value, SwiftUI automatically writes that value to UserDefaults. Luxury! So the cool part is, if you define the timeIndex using the AppStorage property wrapper, then pass that to the second view as a binding, then whenever you change the value (either in view 1, or in view 2) the timeIndex is automatically saved to UserDefaults. Sweet!
|
|
Hi Are you changing the @AppStorage when you change the picker? If so the Picker requires <Binding<>> for it so you can use it instead of
|
|
Thanks everyone for your answers. @roosterboy I get the point of using .onAppear for the picker. But it don't let me select a new value while the picker is shown. @NigelGee It would work, but I want to have the save button to change the AppStorage value. @Obelix I changed my code accordingly. And it works, but the same as the solution from NigelGee. Due to the binding, it changes immediately. I would like to change the value only after clicking the save button. Do you have a solution for that? |
|
Okay, yeah, that's because the Honestly, if you aren't going to make use of the
|
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!
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.