我需要让SwiftUI按钮在当前焦点组件上执行Objective-C选择器。以下方式可行:
let button = Button(action: {
let command = #selector(NSStandardKeyBindingResponding.selectAll(_:))
NSApplication.shared.currentEvent?.window?.firstResponder?.doCommand(by: command)
}, label: { Text("Select All") })
...但这显然非常丑陋。SwiftUI有一个Command
结构体,可以包装一个Selector
,但不清楚在创建命令后如何实际调用选择器。
当按下按钮时,发出任意Command
的推荐方法是什么?
MyFocsedView().onCommand(Command.copy) { print("copy!") }
这样的标准命令,至少在 beta3 中也无法实际执行。 - marcprux