欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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("取消")))
        }
    }
}
相关标签: iOS 开发