使用 confirmationDialog 打造流畅的确认体验
了解如何使用 iOS15 开始新推出 confirmationDialog 修饰器,打造更流畅的用户确认操作体验。

使用 SwiftUI 开发 App,alert
和 confirmationDialog
都可以用于显示需要用户确认的重要选择。
根据苹果的最佳实践,警告信息推荐使用 alert
,确认对话框更推荐使用 confirmationDialog
。
.alert
0:00
/0:05
.alert("确定要删除这部电影吗?", isPresented: Binding(
get: { movieToDelete != nil },
set: { if !$0 { movieToDelete = nil } }
)) {
Button("删除", role: .destructive) {
if let movie = movieToDelete {
withAnimation {
modelContext.delete(movie)
movieToDelete = nil
}
}
}
Button("取消", role: .cancel) {
movieToDelete = nil
}
} message: {
if let movie = movieToDelete {
Text("《\(movie.title)》将被永久删除")
}
}
.confirmationDialog
0:00
/0:06
.confirmationDialog(
"确定要删除这部电影吗?",
isPresented: Binding(
get: { movieToDelete != nil },
set: { if !$0 { movieToDelete = nil } }
),
titleVisibility: .visible
) {
Button("删除", role: .destructive) {
if let movie = movieToDelete {
withAnimation {
modelContext.delete(movie)
movieToDelete = nil
}
}
}
Button("取消", role: .cancel) {
movieToDelete = nil
}
} message: {
if let movie = movieToDelete {
Text("《\(movie.title)》将被永久删除")
}
}