swift - onDeleteCommand 菜单项始终启用
问题描述
我正在将 macOS Monterey Beta 5 与 Xcode 13 Beta 5 一起使用,但onDeleteCommand
. 基本上,即使没有选择任何项目,它的菜单项也不会禁用。
我试过deleteDisabled
了,但它没有帮助。
以下是如何重现:
- 创建一个新的空白 SwiftUI macOS 项目
- 粘贴以下代码:
struct ContentView: View {
@State var data = ["Item1", "Item2", "Item3"]
@State var selection: Set<String> = []
var body: some View {
NavigationView {
List(data, id: \.self, selection: $selection) { item in
Text(item)
}
.onDeleteCommand {
data.removeAll(where: selection.contains) // Removes selected items
selection.removeAll()
}
.deleteDisabled(selection.isEmpty) // This doesn't help either
Text("Second Panel")
}
}
}
- 运行应用程序,然后选择一个项目。
- 转到菜单,然后按编辑 -> 删除。现在选定的项目应该消失了,但编辑 -> 删除菜单项仍处于启用状态。
我能做些什么来解决这个问题(让菜单项自行禁用)?
任何帮助将不胜感激。
解决方案
文档没有说明这一点(我提交了一份报告),但您可以将nil
操作作为操作传递给大多数(全部?)on*Command(perform:)
修饰符以显式禁用关联的菜单项。这个稍微重写ContentView
的应该得到正确的行为:
struct ContentView: View {
@State var data = ["Item1", "Item2", "Item3"]
@State var selection: Set<String> = []
var body: some View {
NavigationView {
List(data, id: \.self, selection: $selection) { item in
Text(item)
}
.onDeleteCommand(perform: selection.isEmpty ? nil : deleteSelection)
Text("Second Panel")
}
}
private func deleteSelection() {
data.removeAll { selection.contains($0) }
selection.removeAll()
}
}
虽然效果可能与接受的答案基本相同,但传递nil
而不是动作块通常是一种更可靠的方法,因为视图身份的更改有时会产生令人惊讶的后果。
推荐阅读
- git - 无法从 Jenkins 连接到 TFS 2015 git 存储库
- php - 使用 jQuery 从动态创建的项目中获取选定的列表值
- reactjs - 如何通过组件键道具滚动到组件?
- git - Git 和 GitHub 使用问题
- spring - 引起:org.springframework.expression.spel.SpelParseException: Expression [] @31: EL1043E: Unexpected token。预期为“冒号(:)”但为“rcurly(}
- php - PHP 和 Shopify API 连接
- php - Wordpress 加载子页面而不刷新,并在上一页的内容下添加子页面内容
- excel-formula - 查找和替换:“替换为”包含美元符号 ($)
- javascript - 我是否需要 javascript 才能将导入的 CSV 字段转换为 Acrobat PDF 中的列表?
- elasticsearch - 自定义停用词分析器工作不正常