Access @ObservedObject object from SwiftUI class into UIKit class and update it

293 Views Asked by At

I am trying to access the @ObservedObject object from SwiftUI class into UIKit class and trying to update the value as well. When I update the value from UIKit class, then it is not receiving broadcast to SwiftUI class.
Here is my code snippet:

In SwiftUI

public class HomeNavBar : ObservableObject{
    @Published var leftButton:Bool = false
    @Published var rightBottom:Bool = false
}


public struct HomeView : View{

@ObservedObject var model: HomeNavBar

public init(model: HomeNavBar){
   self.model = model
}

public var body: some View{
    
    ZStack(alignment: .bottom) {
        
        VStack{
            if model.leftButton{
                print("add left button related code")               
          }
        }
        .edgesIgnoringSafeArea(.bottom)
    }
    
}

In UIKit (swift) class

import SwiftUI
struct MyNavBarConfigurator {
   private var model = HomeNavBar()
   func show(){
      model.leftButton = true //this should invoke the body in HomeView class of SwiftUI
   }
}

Am I doing anything wrong here? Thanks in advance.

0

There are 0 best solutions below