In SwiftUI, Alert is a view, a popup that appears to the user with a title, message, and a button. Buttons were added for the user to interact with this view and we can know their option.
We can show this popup in different usage situations, such as if we want the user to confirm if they really want to exit our app.
Using an alert in SwiftUI requires the .alert modifier.
This modifier expects two parameters:
The first parameter is isPresented and expects a property of type Bool of @State which we will now create. We will give it this property, which is a boolean, and when we change the value of the property to true in the execution of our app, such as when pressing a button, the notice will appear. And the Alert will disappear when the property value is false. When the user clicks the OK button, we change the value of isPresented in this case.
Content: This is the parameter in which we must create our alert. In this case, we use an initializer that expects a title, message, primary button, and secondary button.
struct ContentView: View {
@State var isPresented: Bool = false
var body: some View {
Text("Suscríbete a SwiftBeta")
.padding()
Button("Aceptar") {
isPresented = true
}
.alert(isPresented: $isPresented, content: {
Alert(title: Text("Suscríbete a SwiftBeta"),
message: Text("Cada semana sacamos nuevo video en Youtube"),
primaryButton: Alert.Button.default(Text("Aceptar"), action: {
print("El user ha pulsado el botón de Aceptar")
}),
secondaryButton: .destructive(Text("Cancelar")))
})
}
}
In SwiftUI, Alert is a view, a popup that appears to the user with a title, message, and a button. Buttons were added for the user to interact with this view and we can know their option.
In SwiftUI Alert is a view, a popup that appears to the user with a title, message and a button. Buttons were added for the user to interact with this view and we can know their option. We can show this popup in different usage situations, such as if we want the user to confirm if they really want to Show an alert with a single button
SwiftUI allows us to display alarms to the client with its alert() modifier, but the way it works depends on whether you’re focusing on iOS 15 and later or if you also want to support iOS 13 and 14. I’ll show you the two methodologies here; However, the latest iOS 15 methodology is ideal as it expands on the standard SwiftUI buttons.
We should first check out the method for iOS 15. To display an alarm, create a boolean expression that decides whether the alarm should be noticeable, then connect it to a ready() modifier next to each of the buttons you want to display in the warning. All buttons are ready when touched, so you can assign an empty activity to the basic excuse.
Show an alert with a single button
SwiftUI allows us to display alarms to the client with its alert() modifier, but the way it works depends on whether you’re focusing on iOS 15 and later or if you also want to support iOS 13 and 14. I’ll show you the two methodologies here; However, the latest iOS 15 methodology is ideal as it expands on the standard SwiftUI buttons.
We should first check out the method for iOS 15. To display an alert, create a boolean expression that decides whether the alarm should be noticeable, then connect it to a ready() modifier next to each of the buttons you want to display in the warning. All buttons are ready when touched, so you can assign an empty activity to the basic excuse.
struct ContentView: View {
@State private var showAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert("Important message", isPresented: $showAlert) {
Button("OK", role: .cancel) { }
}
}
}
To demonstrate this, the main method is to characterize some kind of bindable condition that decides whether the alert should be evident or not. Then, at that point, you couple that to your fundamental vision, which presents the alert when its condition turns out to be valid. For example, this source code creates a display alert boolean that tracks whether the “Drink Water” message should be displayed, sets that boolean when a button is tapped, and then at that point creates and connects an alert view using that boolean for what appears when the button is touched:
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message: Text("Drink Water"), dismissButton: .default(Text("Got it!")))
}
}
}
The second way to approach generating alerts is to link to some discretionary express that conforms to the Identifiable protocol, which will cause the alarm to be displayed every time the value of the item changes.
There are two benefits to this strategy:
You can plug in any item you want at runtime, so your alert can display quite a few different bits of information.
SwiftUI naturally opens discretion when it has esteem, so you need to be sure it exists when you need to show caution; Don’t bother checking and opening the value yourself.
For example, this shows an alert with two unique qualities depending on the chosen soccer player:
struct FootballPlayer: Identifiable {
var id: String { name }
let name: String
}
struct ContentView: View {
@State private var selectedShow: FootballPlayer?
var body: some View {
VStack(spacing: 20) {
Text("What is your favorite TV show?")
.font(.headline)
Button("Select Ronaldo") {
selectedShow = FootballPlayer(name: "Ronaldo")
}
Button("Select Messi") {
selectedShow = FootballPlayer(name: "Messi")
}
}
.alert(item: $selectedShow) { show in
Alert(title: Text(show.name), message: Text("Great choice!"), dismissButton: .cancel())
}
}
}
Leave a Reply