我正在开发 macOS 13 应用程序,并且正在使用新的NavigationSplitView. 问题是它不允许我们使用.onDeleteCommand(perform:)(或者也许我用错了)。这是我所做的:
为了使用.onDeleteCommand(perform:),视图需要聚焦。我做了一个简单的应用程序,显示 3 个矩形,我可以使用TAB键进行选择,当我按DELETE键或在菜单栏中的“编辑”>“删除” (两者都会触发.onDeleteCommand)时,它会切换为白色或原始颜色。
VStack {
Rectangle()
.fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
.padding()
.focusable()
.focused($focusedColor, equals: .blue)
Rectangle()
.fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
.padding()
.focusable()
.focused($focusedColor, equals: .red)
Rectangle()
.fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
.padding()
.focusable()
.focused($focusedColor, equals: .yellow)
}
.onDeleteCommand {
if let focusedColor {
if !isColorDeleted.contains(focusedColor) {
isColorDeleted.append(focusedColor)
} else {
let idx = isColorDeleted.firstIndex(of: focusedColor)!
isColorDeleted.remove(at: idx)
} …Run Code Online (Sandbox Code Playgroud)