SwiftUI Alert Not Exhibiting on Subsequent Makes an attempt in iOS App
I am experiencing a difficulty with SwiftUI alerts in my health timer app. When a consumer faucets the pause button throughout a exercise, I wish to present a affirmation alert earlier than truly pausing. The issue is that the alert solely seems the primary time the pause button is tapped. On subsequent makes an attempt, the alert state variable adjustments accurately (verified with print statements), however the alert does not visually seem.
EDIT: I’ve since found that the alert seems when the view hierarchy refreshes (e.g. returning to the app from the house display screen). I tried a repair to the code under utilizing UUID to drive a UI refresh however this is not working both
What I’ve tried:
- Utilizing
.confirmationDialog()
– Similar situation, solely exhibits as soon as - Utilizing
.alert()
– Similar situation, solely exhibits as soon as - Implementing a customized alert overlay with ZStack – This truly works accurately each time, however I might choose to make use of the native alert for higher system integration
Code:
struct WorkoutControlsView: View {
// Different properties...
@State non-public var showAlert = false
@State non-public var dialogReady = true
var physique: some View {
VStack {
// Different UI components...
Button(motion: {
if isPlaying {
print("Pause button tapped, isPlaying = (isPlaying)")
if dialogReady {
dialogReady = false
showAlert = true // This state adjustments accurately (verified with print)
print("Alert triggered, showAlert = (showAlert)")
}
} else {
onPlayPauseTapped()
}
}) {
Picture(systemName: isPlaying ? "pause.circle.fill" : "play.circle.fill")
// Button styling...
}
// Different UI components...
}
.alert(isPresented: $showAlert) {
Alert(
title: Textual content("Pause Exercise?"),
message: Textual content("Are you certain you wish to pause your exercise?"),
primaryButton: .harmful(Textual content("Pause")) {
onPlayPauseTapped()
dialogReady = true
},
secondaryButton: .cancel(Textual content("Proceed")) {
dialogReady = true
}
)
}
// Different view modifiers...
}
}
Debug logs:
Play button tapped, isPlaying = false
isPlaying modified to true
Pause button tapped, isPlaying = true
Alert triggered, showAlert = true
// Alert seems right here
Pause confirmed in alert
isPlaying modified to false
Play button tapped, isPlaying = false
isPlaying modified to true
Pause button tapped, isPlaying = true
Alert triggered, showAlert = true
// Alert ought to seem right here however does not
The alert state is certainly altering to true
on subsequent makes an attempt, however the alert does not visually seem. Apparently, a customized alert implementation utilizing ZStack works completely, however I might choose to make use of the native alert for higher system integration.
Is that this a recognized SwiftUI situation? Are there any workarounds to make the native alert or affirmation dialog seem persistently?
Setting:
- iOS 17.4
- Xcode 15.3
- SwiftUI