【发布时间】:2021-12-31 03:09:28
【问题描述】:
我在我的程序中发现了有趣的动作:
struct Test: View {
@State private var redButton: Bool = false
var body: some View {
List {
ForEach(1...10, id: \.self) { numbers in
Button {
redButton = false
} label: {
Text("Button \(numbers)")
}.contextMenu {
Button {
//action code
redButton = true
} label: {
Text("Deactivate")
}.disabled(redButton)
}
}
}
}
}
如果您运行此代码并在 contexMenu 中按“停用”,contexMenu 将仅对 6..10 按钮禁用,此代码会随机关闭/打开 contextMenu 元素(尝试增加或减少列表元素并按“停用”随机列表元素)。 如果 U 删除 List all 使用一个按钮正常工作。 更改 redButton 状态时,也许我需要使用 dispatchQueue.main.async ? 我做错了什么?
【问题讨论】:
-
你能解释一下你到底想达到什么目标吗?
-
如果你在上下文菜单中选择红色,每行都有“优先状态”(红色、黄色、绿色),之后你不能再次选择该行的红色状态(上下文菜单红色禁用)
-
我很抱歉,但这似乎是错误的 contextMenu,因为我使用 Menu 而不是 contextMenu 并且它工作正常。
标签: swiftui swiftui-list