|
I am fooling around with latest Observable macro in iOS 17. Everything works, macro automatically annotates my Observable ViewModel properties as @ObservationTracked and I can just reference viewmodel properties in my Text views inside my view, even without explicitily using $ symbol. Nice. However my alert sheet for whathever reason cant be binded to viewmodel property "showingAlertNoHealthDataAccess". Just for reference. Here is a piece of my code:
and my view
problem arises on line .alert... as I said
all other properties are successfuly binded to my Text views. All without explicity having to annotate them @Published |
|
@Stepan doesn't provide a lot of detail. So this is a guess.
Have you reviewed @twoStraws' latest sessions on You may need this wrapper to tell a view that the variable you're using can be bound to an object defined elsewhere. |
|
Yes, sorry. English isnt my first language. Thing is... my viewModel is instantiated on App level and passed around via Environment wrapper. But in my App declaration it is defined with @Bindable wrapper like so. I will check that SwiftData tutorial to be sure
|
|
In SwiftUI, you can use the $ symbol to create a binding between a view and a property of its view model. For example, if you have a showingAlert property in your view model, you can bind it to an alert sheet like this: .alert(isPresented: $viewModel.showingAlert) { Alert(title: Text("Alert"), message: Text("This is an alert."), dismissButton: .default(Text("OK"))) } |
|
Hey @poolsapk, I know, but thats not what problem here is. I am no longer using ObservableObject for my ViewModel, but rather, trying to use new Observable macro that was introduced with iOS 17. And that should make all my ViewModel properties published (emitting values over time) automatically as soon as I hook those properties with my views. However for whathever reason, it doesnt work for my alert sheet. |
|
It seems like you're having an issue with binding the showingAlertNoHealthDataAccess property to your alert sheet. The error message you're encountering, "Cannot convert value of type 'Bool' to expected argument type 'Binding<Bool>'," is because the .alert modifier expects a Binding<Bool> for the isPresented parameter. To resolve this, you can create a computed property that returns a binding to showingAlertNoHealthDataAccess. Here's how you can do it:
By creating the alertBinding computed property, you're providing the expected Binding<Bool> to the .alert modifier, and it should bind correctly to your showingAlertNoHealthDataAccess property. This should resolve the issue you're facing. Source:checkmepcobill.pk/fesco-online-bill/ |
|
Thanks very much @checkmepcobill. Turns out I couldnt use @Published propertyWrapper for |
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.