|
I have a method in my class that opens a map when given an address string. Trying to show an alert in a view by toggling a boolean in a method in the class. If addressString is inValid. I can't figure out how to toggle the boolean in the class method. This is what I tried. The Published bool in class method updates but does not update in the View. I did put up a repo of just this feature if anybody wants to play around with it. https://github.com/Ongomobile/OpenMapInSwiftUI
} Here is my Class:
Here is the view:
|
|
the alert in your view needs to be isPresented from your Bool in your class not the one in your view change: .alert(isPresented: $showingAlert) to: .alert(isPresented: locationManager.!isValid) (exclamation mark may go in fromt of locationManager rather than isValid, I can't remember :) |
|
Hi @rlong405 thanks for the help but that did not work for me. I did put up a repo of just this feature if anybody wants to play around with it. |
|
This is what I can up with that worked could be refactored to be more efficient.
} Here is the updated ViewModel
|
|
How about this..... puts everything back in your class and don't need the getMap func any more. The issue with my original suggestion is that (as I've now found out) - you can't bind the isPresented of a .alert to a negative value. So getting it to fire when isValid = false doesn't seem to be possible (someone else may know a way). Anyway, changing your "isValid" bool so it's called "invalid" and then modifying your logic so it works the opposite way around (i.e. if it can't find the address then it sets the invalid bool to true) seems to work. Alert gets fired or maps get loaded as I would expect. I've also bound your textfield directly to the locationString in your class rather than the one in your view. hope that helps - yours to adopt or trash at your leisure but I think this fits your original ask without having the additional function you created to set a second bool value which as you say is a little unecessary
and your view:
|
|
@rlong405 Thank you very much my friend very nice refactor. This really helps me to get a better grip on SwiftUI. I replaced the repository with a new one and commited changes giving you credit for the refactor https://github.com/Ongomobile/OpenMapInSwiftUI Maybe others could use this. Cheers, Mike Haslam |
|
Hey Mike thats very kind of you thankyou for the github credit (my first). still learning swiftui myself and self taught but ive been doing quite a bit of View Model work recently so felt I could add some help here. plus I learnt something too about the isPresented thing so thats a bonus best of luck with your project Rich |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
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.