How can I reinitialize a UIViewRepresentable when a value changes?

99 Views Asked by At

So let's say we have the following code:

struct MyView: UIViewRepresentable {

    @Binding var myValue: Int
        
    func makeUIView(context: Context) -> MyOtherView {
        let someValue = SomeController()
        
        return MyOtherView(someValue: someValue)
    }

    func updateUIView(_ uiView: MyOtherView, context: Context) {
        print(myValue)
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject {
        var parent: MyView
        
        init(_ parent: MyView) {
            print("init() called")
            self.parent = parent
        }
    }
}

When the value myValue changes in my ContentView I need to reinitialize MyView, but the init()-method is only called once, when first running the app. It never gets called when I change the binding variable myValue. What am I doing wrong here?

0

There are 0 best solutions below