SwiftUI的Alert使用方式
记录一下SwiftUI的Alert使用方式,比较简单直接上代码
import SwiftUI
struct AlertBootCamp: View {
@State var showAlert = false
var body: some View {
Button {
showAlert.toggle()
} label: {
Text("alert show")
}
/// 单按钮
// .alert(isPresented: $showAlert, content: {
// Alert(title: Text("this is a alert!"))
// })
.alert(isPresented: $showAlert, content: {
Alert(title: Text("title"), message: Text("message"), primaryButton: .default(Text("sure"), action: {
print("sure")
}), secondaryButton: .cancel(Text("cancel"), action: {
print("cancel")
}))
})
}
}
#Preview {
AlertBootCamp()
}
效果图