如何使用 SwiftUI 显示警报
How to present an Alert with SwiftUI
在SwiftUI
中我发现了Alert
类型。但我想知道如何用 presentation
方法显示它。
初始化 Alert
非常简单。但是如何使用绑定呢?
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")
})
}
}
绑定的类型为 Binding<Bool>
您可以使用 @State
变量作为绑定。或者,您可以使用 @EnvironmentObject
变量,该变量使用 BindableObject
.
我认为您需要在根视图上调用 presentation
才能使其工作,将其添加到 Stack
、Group
等似乎不起作用.
这段代码似乎可以解决问题。请注意,@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"))
})
}
}
struct ContentView: View {
@State var aAlert = false
var body: some View {
Text("Alert").tapAction {
self.aAlert = true
}.presentation($aAlert, alert:{ Alert(title: Text("Alert"))})
}
}
除了@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()
)
}
}
结果:
.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"))
}
}
}
Full Code of Alert with dismiss and okay action:
代码:
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(title: Text("Alert title"), message: Text("Alert message"), dismissButton: .default(Text("Got it!")))
然后定义一个可绑定的条件,告知警报何时可见或不可见。将该条件切换为 show/hide 警报。
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!")))
}
}
}
示例 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")))
})
}
}
除了@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)
}
}
}
用法:
所以您现在可以像这样轻松地使用它:
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
}
}
这是显示多个 警报的解决方案。适用于 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"))
}
}
}
}
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert("Important message", isPresented: $showingAlert) {
Button("First") { }
Button("Second") { }
Button("Third") { }
}
}
}
在SwiftUI
中我发现了Alert
类型。但我想知道如何用 presentation
方法显示它。
初始化 Alert
非常简单。但是如何使用绑定呢?
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")
})
}
}
绑定的类型为 Binding<Bool>
您可以使用 @State
变量作为绑定。或者,您可以使用 @EnvironmentObject
变量,该变量使用 BindableObject
.
我认为您需要在根视图上调用 presentation
才能使其工作,将其添加到 Stack
、Group
等似乎不起作用.
这段代码似乎可以解决问题。请注意,@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"))
})
}
}
struct ContentView: View {
@State var aAlert = false
var body: some View {
Text("Alert").tapAction {
self.aAlert = true
}.presentation($aAlert, alert:{ Alert(title: Text("Alert"))})
}
}
除了@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()
)
}
}
结果:
.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"))
}
}
}
Full Code of Alert with dismiss and okay action:
代码:
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(title: Text("Alert title"), message: Text("Alert message"), dismissButton: .default(Text("Got it!")))
然后定义一个可绑定的条件,告知警报何时可见或不可见。将该条件切换为 show/hide 警报。
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!")))
}
}
}
示例 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")))
})
}
}
除了@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)
}
}
}
用法:
所以您现在可以像这样轻松地使用它:
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
}
}
这是显示多个 警报的解决方案。适用于 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"))
}
}
}
}
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button("Show Alert") {
showingAlert = true
}
.alert("Important message", isPresented: $showingAlert) {
Button("First") { }
Button("Second") { }
Button("Third") { }
}
}
}