struct ContentView: View {
@State var isError: Bool = false
var body: some View {
Button(action:{
isError = true
}){
Text("Alertテスト")
}.alert(isPresented: $isError){
Alert(title: Text("タイトル"), message: Text("メッセージ文"),
dismissButton: .default(Text("OK"), action: {}))
}
}
}
OKとキャンセルがあるアラート
struct ContentView: View {
@State var isError: Bool = false
var body: some View {
Button(action:{
isError = true
}){
Text("Alertテスト")
}.alert(isPresented: $isError){
Alert(title: Text("タイトル"), message: Text("メッセージ文"),
primaryButton: .default(Text("OK"), action: {
okAction()
}),
secondaryButton: .cancel(Text("キャンセル"), action:{})
)}
}
}
func okAction(){
print("OKボタンが選ばれた")
}
primaryButtonとsecondaryButtonを指定する
struct ContentView: View {
@State var isError: Bool = false
var body: some View {
Button(action:{
isError = true
}){
Text("Alertテスト")
}.alert(isPresented: $isError){
Alert(title: Text("タイトル"), message: Text("メッセージ文"),
primaryButton: .destructive(Text("削除する"), action: {}),
secondaryButton: .cancel(Text("キャンセル"), action:{})
)}
}
}
やべーな、swiftかなり舐めてたわ…orz