There is an array of Views, and whenever the currentPosition variable changes, the view must change. However it is not changing.
I have a global ObservableObject class that has an array of Views, and a currentPosition value. My goal is to have the view changed according to the currentPosition variable.
class AXGlobalValues: ObservableObject {
@Published var tabs: [AXTabItem] = [
.init(webView: AXWebView()),
.init(webView: AXWebView())
]
@Published var currentPosition: Int = 0
}
On the SwiftUI view, I wrote global.tabs[global.currentPosition].webView, and I was expecting the webView to change based on the global.currentPosition value. However it didn't change.
What I tried to do was add an onChange(of:), but since SwiftUI is a declarative language, I couldn't update the view.
struct AXTabView: View {
@EnvironmentObject private var global: AXGlobalValues
var body: some View {
Text("\(global.currentPosition)")
global.tabs[global.currentPosition].webView
.onChange(of: global.currentPosition) { newValue in
// Not allowed in SwiftUI :(
global.tabs[global.currentPosition].webView
}
}
}
Is there any way I can update the view based on the currentPosition variable?
Thanks in advance!