SwiftUI - How to use onCommand with NSMenuItem on macOS

前端 未结 2 835
悲&欢浪女
悲&欢浪女 2021-02-06 07:42

I am trying to find the best solution to connect a NSMenuItem with SwiftUI onCommand on macOS.

Currently I an doing the following:

  1. In AppDelegate I create
2条回答
  •  旧时难觅i
    2021-02-06 08:12

    I ended up creating a PassthroughSubject in my AppDelegate, passing it through to my SwiftUI view, and subscribing to it in the model object owned by my SwiftUI view. When my AppDelegate selector for the menu command is called, I send the event using the PassthroughSubject, which my model receives and handles appropriately.

    My answer is definitely not ideal, and I'd love to know if you figure out a better way to do this.

提交回复
热议问题