SwiftUI Alert
程序员文章站
2024-03-24 12:46:10
...
单个选项的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!"))
)
}
}
}
两个且有action的alert
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}) {
Text("清除回收站")
}
.alert(isPresented: $showAlert) {
Alert(title: Text("你确定要清空回收站吗?"),
message: Text("任务将无法恢复"),
primaryButton: .destructive(Text("确定")) {
for todo in self.userData.userTodos {
if todo.isDeleted {
let todoIndex = self.userData.userTodos.firstIndex(where: {$0.id == todo.id})!
self.userData.userTodos.remove(at: todoIndex)
}
}
},
secondaryButton: .cancel(Text("取消")))
}
}
}
上一篇: 四、transition过渡和animation动画
下一篇: 图片缩放