简体   繁体   中英

How to notify a Swift UI view after a coordinator handles delegate call?

I have a class MyCoordinator which implements SomeDelegate :

class MyCoordinator: NSObject {
    
}

extension MyCoordinator: SomeDelegate {
    func didReceiveSomeData(someData: SomeData) {
        //...someData is needed for the MyControllerRepresantable
    }
}

In a BasicView , I need to start MyControllerRepresantable (which is a UIViewControllerRepresentable ) with the received data:

struct BasicView: View {
//...
    var body: some View { 
//...
        NavigationLink(destination: MyControllerRepresantable(data: someData), isActive: $isNavActive) {}
    }
}

What would be the best way to do it? Maybe I could somehow observe someData so that when it is set, the View gets notified and can access it?

you can store your View instance in your Coordinator and then notify your view by setting a Binding or calling a method.

class Coordinator: NSObject {
        let controllerRepresantable: MyControllerRepresantable

        init(_ controllerRepresantable: MyControllerRepresantable) {
            self.controllerRepresantable = controllerRepresantable
        }

now you can notify your controllerRepresantable using a callback method or a binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM