【发布时间】:2022-12-09 19:29:46
【问题描述】:
我正在开发 macOs 13 应用程序,我正在使用新的NavigationSplitView。问题是它不允许我们使用.onDeleteCommand(perform:)(或者我可能用错了)。这是我所做的:
为了使用.onDeleteCommand(perform:),需要聚焦视图。我做了一个简单的应用程序,显示了 3 个矩形,我可以使用标签键,当我按下删除键或在菜单栏中编辑 > 删除(都触发.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)
}
}
}
^^^ 这可以正常工作 ^^^
但是如果你像这样把它放在NavigationSplitView中:
NavigationSplitView(columnVisibility: $visibility) {
List {
Text("Main page")
}
} detail: {
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)
}
}
}
}
如果你按删除或者编辑 > 删除当一个矩形像我解释的那样被聚焦时,它什么也没有。事实上,编辑 > 删除根本不可点击。
【问题讨论】:
-
你能解释一下它是如何不起作用的吗?
-
我添加了一些细节,主要是在最后 @malhal stackoverflow.com/posts/74429687/revisions
-
@MaxAuMax 我遇到了同样的问题。我的代码有更多内容,在侧边栏和详细视图中有一个删除。当我打印到控制台时,我看到删除只考虑侧边栏项目上的焦点,并且永远不会触发详细视图......
-
我目前没有修复程序,它困扰着我,很可能是一个框架错误(在我更改为新导航之前曾经工作过)而且我的代码再简单不过了
.onDeleteCommand(perform: { print("Delete command received from DETAIL!") })而且这永远不会被打印出来