Hi, I've got swiftUI based project that gets events from objc based library.

The objc event generator part have method that register callbacks from the swift UI part.

@implementation eventGenerator
typedef void (^eventHandler)(NSDictionary * data);

-(void)registerUIEvent1:(eventHandler)eventCallback1
                          Event2:(eventHandler)eventCallback2;

// trigger the callback of the appropriate event.  
-(void)event1;
-(void)event2;

@end

in the swift side I defined the closures to be registered for event1 and even2, and I'd like those closure to modify the View in the swiftUI part. So in event1 handler, I try to access showAlert member of instance alert_handler inside the ContentView.


init.swift
-----------

func applicationDidFinishLaunching(_ aNotification: Notification) {

let event1Handler : eventHandler = {(data) in
    ContentView().alert_handler.showAlert = true
}
let event2Handler : eventHandler = {(data) in
    ...
}

eventGenerator.sharedInstance().registerUIEvent1(event1Handler, event2: event2Handler)

notice that in the swiftUI part, my alert is presented depending on $alert_handler.showAlert.

ContentView.swift
------------------------
class alertHandler: ObservableObject {
    @Published var showAlert = false
}

struct ContentView: View {
     @ObservedObject var alert_handler = alertHandler()
     var body: some View {
         GeometryReader { metrics in
         .....
         }.alert(isPresented: $alert_handler.showAlert, content: {
            Alert(title: Text("Alert:"),
                    message: Text("press OK to execute default action..."),
                    dismissButton: Alert.Button.default(
                          Text("Press ok here"), action: { print("Hello world!") }
          )
      )
    })

Unfortunately I couldn't see the alert appears when the first event was triggered. Perhaps anyone can tell me if I'd doing anything wrong ? or suggest an alternative approach to modify swiftUI struct variable (event_handler) from remote closure ?

I believe that my the problem may derived from ContentView module which is not a singleton, so when I set showAlert I'm doing so for another instance of ContentView. How can I fix my code to access showAlert that belongs to the currently running instance of ContentView ?

Thanks !

You code is a bit confusing. You have

let event1Handler : eventHandler = {(data) in
    ContentView().alert_handler.showAlert = true
}

It creates a ContentView sets a property on it, and nothing else — the ContentView should be in the body of a hosted SwiftUI view (either using NS/UIHostingController, or the default @main App you get when you create a SwiftUI project.)

You were right. thanks.

What i did eventually is setting the alertHandler as globla variable and pass it to the view

var handler = alertHandler()

class alertHandler: ObservableObject {
    @Published var showAlert = false
}
...
..
.
window.contentView = NSHostingView(rootView: ContentView(alert_handler: handler))

inside the ContentView, I defined a reference to this value

struct ContentView: View {
     @ObservedObject var alert_handler : alertHandler //<-- Here

This way, when I modify the showAlert property inside alertHandler, the ContentView gets updated because it's the same instance of alertHandler.