SwiftUI - Alert Example.

This tutorial is to display Alert in SwiftUI. As you know in Swift we are using UIAlertController to  display alert popup over the screen. Now here in SwiftUI we have a class name Alert to do some thing. Here we are study how to play with alert via SwiftUI and How handle the action. first you create SwiftUI Project.




You are study to "UIAlertController in swift", Now this tutorial for Alert in SwiftUI.

Before the tutorial you will check "SwiftUI Text" and "SwiftUI Button", because In this tutorial we are use also "Text and "Button".

The Alert have Dismiss and Okay Action.

Full Code of Alert SwiftUI:
//  Created by Kirit Modi on 17/09/19.
//  Copyright © 2019 iOSDevCenters. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @State private var isAlert = false

    var body: some View {
            Button(action: {
                self.isAlert = true
            }) {
                Text("Click Alert")
                .foregroundColor(Color.white)
            }
            .padding()
            .background(Color.blue)
            .alert(isPresented: $isAlert) { () -> Alert in
                Alert(title: Text("iOSDevCenters"), message: Text("This Tutorial for SwiftUI Alert."), primaryButton: .default(Text("Okay"), action: {
                    print("Okay Click")
                }), secondaryButton: .default(Text("Dismiss")))
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Output:

SwiftUI - Alert Example. SwiftUI - Alert Example. Reviewed by KIRIT MODI on 22:45:00 Rating: 5

No comments:

Powered by Blogger.