【发布时间】:2021-06-29 07:45:01
【问题描述】:
在我的 swiftUI MacOS 应用程序中,我通过将绑定到警报的 State 变量设置为 true 来显示警报。但是在解除警报后,它会再次显示,我必须再次解除它。
我确实看到帖子here 有同样的问题,但似乎没有结论。
这里是警报:
.alert(isPresented: $showingCantDeleteInstanceAlert) {
Alert(
title: Text("Unable to delete instance \"\(instances[selectedInstanceIndex ?? 0].wrappedName)\""),
message: Text("Can't have zero instances. Create a new instance to delete this one."),
dismissButton: .default(Text("OK"))
)
}
下面是设置 State 变量的代码:
if instances.count <= 1 {
showingCantDeleteInstanceAlert = true
return
}
我什至尝试在解除警报时手动将变量设置为 false:
.alert(isPresented: $showingCantDeleteInstanceAlert) {
Alert(
title: Text("Unable to delete instance \"\(instances[selectedInstanceIndex ?? 0].wrappedName)\""),
message: Text("Can't have zero instances. Create a new instance to delete this one."),
dismissButton: .default(Text("OK")) {
showingCantDeleteInstanceAlert = false
}
)
}
但问题依然存在,警报仍然显示两次...
有什么想法吗?
【问题讨论】:
标签: arrays swift binding alert