我想在按钮点击等事件上显示 ActionSheet(或任何其他模式,但 Alert)。
我找到了使用状态变量的方法。以这种方式显示它对我来说似乎有点奇怪,因为我必须在 ActionSheet 手动关闭时重置变量。
有更好的方法吗?
为什么有一个单独的方法来呈现警报,允许您将其可见性绑定到状态变量?我的方法有什么不同?
struct Sketch : View {
@State var showActionSheet = false
var body: some View {
ZStack {
Button(action: { showActionSheet = true }) { Text("Show") }
}
.presentation(showActionSheet ?
ActionSheet(
title: Text("Action"),
buttons: [
ActionSheet.Button.cancel() {
self. showActionSheet = false
}
])
: nil)
}
}