[英]How to present an Alert with SwiftUI
In SwiftUI
I discovered the Alert
type.在
SwiftUI
我发现了Alert
类型。 But I wonder how to show it with the presentation
method.但我想知道如何用
presentation
方法来展示它。
Initializing an Alert
is pretty easy.初始化
Alert
非常简单。 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>
绑定的类型为
Binding<Bool>
.presentation()
was actually deprecated in Beta 4. Here is a version that currently works with the .alert()
Modifier. .presentation()
实际上在 Beta 4 中已被弃用。这是当前与.alert()
修饰符一起使用的版本。
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"))
}
}
}
You can use a @State
variable as the binding.您可以使用
@State
变量作为绑定。 Alternatively you can use a @EnvironmentObject
variable that uses a BindableObject
.或者,您可以使用使用
BindableObject
的@EnvironmentObject
变量。
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.我认为您需要在根视图上调用
presentation
才能使其工作,将其添加到Stack
、 Group
等似乎不起作用。
This snippet seems to do the trick.这个片段似乎可以解决问题。 Note that
@State
variable is set to false after the alert is dismissed.请注意,
@State
变量在警报解除后设置为 false。
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"))
})
}
}
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:输出:
In addition to @tsp's answer, to display an alert with two buttons and handle button tap action, you can do as below:除了@tsp 的回答,要显示带有两个按钮的警报并处理按钮点击动作,您可以执行以下操作:
@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:结果:
Here's the solution for presenting multiple alerts.这是呈现多个警报的解决方案。 Works on iOS14 :
适用于iOS14 :
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"))
}
}
}
}
Example with onTapGesture
使用
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")))
})
}
}
In addition to @thisIsTheFoxe 's answer, you can implement a simple extension:除了@thisIsTheFoxe 的回答之外,您还可以实现一个简单的扩展:
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
}
}
struct ContentView: View {
@State var aAlert = false
var body: some View {
Text("Alert").tapAction {
self.aAlert = true
}.presentation($aAlert, alert:{ Alert(title: Text("Alert"))})
}
}
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!")))
}
}
}
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)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.