In my App i want to initialize an @StateObject property that takes a binding in its initializer. I created an example project to try fixing this problem.
struct ContentView: View {
@StateObject var myClass: MyClass
@State var showColor: Bool = false
init() {
self._myClass = StateObject(wrappedValue: MyClass(showColor: $showColor)) // This does not work
}
var body: some View {
VStack {
if showColor {
Image(systemName: "globe")
.resizable()
.foregroundColor(showColor ? .accentColor : .black)
.frame(width: 50, height: 50)
}
Button {
myClass.doSomething()
} label: {
Text("Do Something")
.padding()
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(10)
}
}
.padding()
}
}
The MyClass class looks like this:
class MyClass: ObservableObject {
@Binding var showColor: Bool
init(showColor: Binding<Bool>) {
self._showColor = showColor
}
func doSomething() {
self.showColor.toggle()
}
}
The doSomething should toggle the showColor of ContentView.
I tried to initialize the variable inside the init function before creating the MyClass Object. Also the approach to use State or StateObject initializer and wrap the value does not work. Furthermore i tried to use weak var and pass the ContentView to myClass but thats not allowed by xCode (SwiftUI).
Now i am at a point where i have no idea how to solve that problem, can anyone help?
You need to change
showColorto@Publishedinside your Class and initialise your class inside the View like this:And instead of using:
Use this: