Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4758

Alert Won't Present in SwiftUI

Here is the code I am using:

struct ContentView: View {
    @State var showingAlert: Bool = false
    var body: some View {
        VStack {
            Button("Show Alert") {
                showingAlert = true
            }
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Important message"),
                  message: Text("Wear sunscreen"),
                  dismissButton: .default(Text("Got it!")))
        }
    }
}

I tried tapping the Show Alert button so many times in the iPad Pro (11-inch) (3rd Generation) simulator but nothing happens. What's wrong in the code above? or is it a bug in the latest simulator? (I am using Xcode 12.5).

Upvotes: 0

Views: 855

Answers (1)

Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4758

As it turns out, I have an alert in the parent view. It seems it blocked the one in the child view. Once I removed the alert from the parent one, the child's alert appeared. Thanks, swiftUI, for being so smart.

Upvotes: 2

Related Questions