Alert
Alert 用于弹窗,其中的 Alert.Button 有如下枚举类型:
- cancel
- default
- destructive
这与 UIKit 是一样的。
示例所示代码:
@State private var showAlert1 = false
@State private var showAlert2 = false
Button {
showAlert1 = true
} label: {
Text("Show Alert 1")
}
.alert(isPresented: $showAlert1) {
Alert(title: Text("Alert 1"), message: Text("message 1"), dismissButton:
.cancel())
}
Button {
showAlert2 = true
} label: {
Text("Show Alert 2")
}
.alert(isPresented: $showAlert2) {
Alert(title: Text("Alert 2"), message: Text("message 2"), primaryButton:
.default(Text("primaryButton")), secondaryButton:
.destructive(Text("secondaryButton")))
}
ConfirmationDialog(ActionSheet)
Alert 最多只支持两个可操作的按钮,如果有更多的操作,就需要用到 ConfirmationDialog 了。其实就是以前的 ActionSheet,不过在 iOS 15.0 已经被废弃了。
@State private var showDialog = false
Button {
showDialog = true
} label: {
Text("Show ConfirmationDialog")
}
.confirmationDialog("ConfirmationDialog", isPresented: $showDialog, actions: {
Button("Option 1", role: .destructive, action: {})
Button("Option 2", action: {})
Button("Option 3", action: {})
Button("Cancel", role: .cancel) {
showDialog = false
}
}, message: {
Text("message")
})