Alert & Action Sheet in SwiftUI
Alerts & Action Sheets do work in similar fashion as they do previously in Swift or Objective C.
Below are the codes to achieve Alert and Action Sheet in SwiftUI.
Both of these do have Title, Message and Buttons (you can add actions to them).
Alert Sheet Code Snippet:
Output:
Action Sheet Code Snippet:
Output:
Below are the codes to achieve Alert and Action Sheet in SwiftUI.
Both of these do have Title, Message and Buttons (you can add actions to them).
Alert Sheet Code Snippet:
struct ContentView: View {
@State var choosenAction = "Hit the button to show Alert"
@State var showAlert: Bool = false
var body: some View {
VStack (spacing: 50) {
Text(choosenAction)
.font(.largeTitle)
.foregroundColor(.green)
Button(action: {
self.showAlert.toggle()
}) {
Text("TOGGLE NAMES")
.padding(.all)
.font(.headline)
.foregroundColor(.white)
.background(Color.red)
.cornerRadius(10)
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Alert"), message: Text("Yeh!!! we have shown Alert"), dismissButton: .default(Text("OK")))
}
}
}
}
Output:
Action Sheet Code Snippet:
struct ContentView: View {
@State var choosenAction = "Hit the button and choose from ActionSheet"
@State var showAlert: Bool = false
var body: some View {
VStack (spacing: 50) {
Text(choosenAction)
.font(.largeTitle)
.foregroundColor(.green)
Button(action: {
self.showAlert.toggle()
}) {
Text("TOGGLE NAMES")
.padding(.all)
.font(.headline)
.foregroundColor(.white)
.background(Color.red)
.cornerRadius(10)
}
.actionSheet(isPresented: $showAlert) {
ActionSheet(title: Text("What do you want to do?"), message: Text("Choose from below actions"), buttons: [.default(Text("OK"), action: {
self.choosenAction = "You selected 'OK' from Action Sheet"
}), .destructive(Text("CANCEL"), action: {
self.choosenAction = "You selected 'CANCEL' from Action Sheet"
})])
}
}
}
}
Output:
Comments
Post a Comment