I'm trying to get into swift/swiftui but I'm really struggling with this one:
I have a MainView
containing a ChildView
. The ChildView
has a function update
to fetch the data to display from an external source and assign it to a @State data
variable.
I'd like to be able to trigger update
from MainView
in order to update data
.
I've experienced that update
is in fact called, however, data
is reset to the initial value upon this call.
The summary of what I have:
struct ChildView: View {
@State var data: Int = 0
var body: some View {
Text("\(data)")
Button(action: update) {
Text("update") // works as expected
}
}
func update() {
// fetch data from external source
data = 42
}
}
struct MainView: View {
var child = ChildView()
var body: some View {
VStack {
child
Button(action: {
child.update()
}) {
Text("update") // In fact calls the function, but doesn't set the data variable to the new value
}
}
}
}
When googling for a solution, I only came across people suggesting to move update
and data
to MainView
and then pass a binding of data
to ChildView
.
However, following this logic I'd have to blow up MainView
by adding all the data access logic in there. My point of having ChildView
at all is to break up code into smaller chunks and to reuse ChildView
including the data access methods in other parent views, too.
I just cannot believe there's no way of doing this in SwiftUI.