|
I am an expeienced professional iOS developer but very inexperienced with SwiftUI, just started using it last week. I have a so-far-simple app that has a SwiftUI wrapper around a As in the example app, I am using a I want to use the navigation buttons to send back and forward navigation commands to the webView. For proper code modularization, I want to pass the
I think this is the proper pattern, is it correct? It works, but I want to write idiomatic SwiftUI so we don't have to go through in six months and redo all the stuff we did when learning. |
|
Actually, after doing more reading on |
|
To pass an observableObject to a childView, you should use a binding. Replace this in your WebNavigationView:
with this:
And update your ContentView to pass in the viewModel to your WebNavigationView:
Now you have full access to your viewModel in the WebNavigationView. More info: https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-binding-property-wrapper |
|
@joosttk Thanks a bunch! Unfortunately that begets the build-time error If I change the |
|
Make sure you use the $-sign when passing in your viewModel to your childview:
|
|
@joosttk I did! I promise. :) If I leave the Copy and paste for verification:
and :
|
|
Ah yes.. makes sense, I was a bit confused. I believe you can just pass it as a normal variable. No need to use State or a Binding.
And
|
|
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
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.