我正在尝试找到将NSMenuItem与macOS上的SwiftUI onCommand连接起来的最佳解决方案。
目前,我正在执行以下操作:
- 在AppDelegate中,我创建了一个虚拟函数,以便能够列出第一响应者接收到的动作列表。
@IBAction func changeColor(_ sender: Any) {
print("Dummy Function")
}
在 Main.storyboard 中创建并连接一个具有 First Responder 功能的 NSMenuItem。
向我的 SwiftUI 视图添加所需的代码。
struct TestView: View {
let changeColor = #selector(AppDelegate.changeColor(_:))
var body: some View {
VStack {
TextField("Text", text: .constant(""))
Text("Hello World!")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onCommand(changeColor) {
print("Change Color Action")
}
}
}
如果TextField是第一响应者,那么我会看到打印出“Change Color Action”的信息。 如果TextField之前不是第一响应者,则视图不会成为第一响应者。也许这是Catalina Beta(19A558d)中当前存在的一个bug,因为我无法使可聚焦性起作用。
NSMenu
? - Faruk