Reputation: 6673
In SwiftUI
I discovered the Alert
type. But I wonder how to show it with the presentation
method.
Initializing an Alert
is pretty easy. But how to use the binding?
struct ContentView : View {
var body: some View {
Button(action: {
// Don't know how to use the `binding` below
presentation(binding, alert: {
Alert(title: Text("Hello"))
})
}, label: {
Text("asdf")
})
}
}
The binding is of type Binding<Bool>
Upvotes: 30
Views: 46984
Reputation: 34225
SwiftUI present Alert
Example with onTapGesture
struct MyRow: View {
@State private var showingAlert = false
var body: some View {
HStack {
Text("Hello")
Text("World")
}
.onTapGesture {
self.showingAlert = true
}
.alert(isPresented: $showingAlert, content: {
Alert(title: Text("Title"), message: Text("Message"), dismissButton: .default(Text("OK")))
})
}
}
Upvotes: 4
Reputation: 1
public extension View {
func alert(isPresented: Binding<Bool>,
title: String,
message: String? = nil,
dismissButton: Alert.Button? = nil) -> some View {
alert(isPresented: isPresented) {
Alert(title: Text(title),
message: {
if let message = message { return Text(message) }
else { return nil } }(),
dismissButton: dismissButton)
}
}
}
Upvotes: -1
Reputation: 6911
Here's the solution for presenting multiple alerts. Works on iOS13-iOS15:
struct YourView: View {
enum AlertType: Identifiable {
case first, second
var id: Int {
hashValue
}
}
@State var alertType: AlertType?
var body: some View {
VStack {
Button("Show alert #1") {
alertType = .first
}
Button("Show alert #2") {
alertType = .second
}
}
.alert(item: $alertType) { type in
switch type {
case .first:
return Alert(title: Text("First alert"))
case .second:
return Alert(title: Text("Second alert"))
}
}
}
}
Upvotes: 10
Reputation: 119302
In addition to @thisIsTheFoxe 's answer, you can implement a simple extension:
public extension View {
func alert(isPresented: Binding<Bool>,
title: String,
message: String? = nil,
dismissButton: Alert.Button? = nil) -> some View {
alert(isPresented: isPresented) {
Alert(title: Text(title),
message: {
if let message = message { return Text(message) }
else { return nil } }(),
dismissButton: dismissButton)
}
}
}
So you can now use it easily like:
struct ContentView: View {
@State var showsAlert = false
var body: some View {
Button("Show Alert") {
self.showsAlert.toggle()
}
.alert(isPresented: $showsAlert, title: "title", message: "Message") // <- Here
}
}
Upvotes: 3
Reputation: 3802
SwiftUI
First create basic alert :
Alert(title: Text("Alert title"), message: Text("Alert message"), dismissButton: .default(Text("Got it!")))
Then define a bindable condition that tells when the alert will be visible or not. Toggle that condition to show/hide the alert.
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}) {
Text("Show Alert")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}
Upvotes: 2
Reputation: 23407
Full Code of Alert with dismiss and okay action:
Code:
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:
Upvotes: 15
Reputation: 1713
.presentation()
was actually deprecated in Beta 4. Here is a version that currently works with the .alert()
Modifier.
struct ContentView: View {
@State var showsAlert = false
var body: some View {
Button(action: {
self.showsAlert.toggle()
}) {
Text("Show Alert")
}
.alert(isPresented: self.$showsAlert) {
Alert(title: Text("Hello"))
}
}
}
Upvotes: 33
Reputation: 1062
struct ContentView: View {
@State var aAlert = false
var body: some View {
Text("Alert").tapAction {
self.aAlert = true
}.presentation($aAlert, alert:{ Alert(title: Text("Alert"))})
}
}
Upvotes: 1
Reputation: 19708
In addition to @tsp's answer, to display an alert with two buttons and handle button tap action, you can do as below:
@State var showAlert = false
var body: some View {
Button(action: {
self.showAlert = true
}) {
Text("Show Alert")
}
.presentation($showAlert) {
Alert(title: Text("Title"), message: Text("Message..."),
primaryButton: .default (Text("OK")) {
print("OK button tapped")
},
secondaryButton: .cancel()
)
}
}
Result:
Upvotes: 6
Reputation: 1976
You can use a @State
variable as the binding. Alternatively you can use a @EnvironmentObject
variable that uses a BindableObject
.
I think you need to call presentation
on the root View to get it to work, adding it to a Stack
, Group
, etc. doesn't seem to work.
This snippet seems to do the trick. Note that @State
variable is set to false after the alert is dismissed.
struct ContentView: View {
@State var showsAlert = false
var body: some View {
Button(action: {
self.showsAlert = true
}, label: {
Text("asdf")
}).presentation($showsAlert, alert: {
Alert(title: Text("Hello"))
})
}
}
Upvotes: 15