When I presented a button to determine whether to pop up or not, if I hit a popup, I presented a popup via.alert(isPresented: $isShowingAlert), and then onDisappear does not appear.
var body: some View {
VStack(alignment: .tiltButtonGuide) {
HStack(spacing: 17) {
Button(action: {
tarotModel.randomTarot.toggle()
}, label: {
Label("随机一张", systemImage: "gamecontroller")
})
// alert弹出会出现bug,导致onDisappear不执行
Button(action: {
if tarotModel.selectedTarot == false {
isShowingAlert = true
// tarotModel.randomTarot.toggle()
// tarotModel.isShowingTarot.toggle()
return
}
tarotModel.isShowingTarot.toggle()
isShowingAlert = false
}, label: {
Label("确定", systemImage: "heart.circle")
})
.alert(isPresented: $isShowingAlert) {
Alert(title: Text("温馨提示"),
message: Text("请选择一张卡牌"),
dismissButton: .default(Text("知道了")))
}
Button(action: {
tarotModel.resetTarot.toggle()
}, label: {
Label("洗牌", systemImage: "arrow.triangle.2.circlepath")
})
}
.toggleStyle(.button)
.buttonStyle(.borderless)
//.labelStyle(.iconOnly)
.padding(12)
.glassBackgroundEffect(in: .rect(cornerRadius: 50))
// .alignmentGuide(.controlPanelGuide) { context in
// context[HorizontalAlignment.center]
// }
.accessibilitySortPriority(2)
}
.onChange(of: tarotModel.isShowingTarot) { _, isShowing in
Task {
if isShowing {
await openImmersiveSpace(id: "ImmersiveSpace")
} else {
await dismissImmersiveSpace()
}
}
}
.onDisappear() {
tarotModel.selectedTarot = false
}