본문 바로가기
iOS/SwiftUI

Alert & ConfirmationDialog

by 소토리텔러 2023. 11. 20.

 

Alert

func alert<A, M>(_ titleKey: LocalizedStringKey,
                              isPresented: Binding<Bool>,
                              @ViewBuilder actions: () -> A,
                              @ViewBuilder message: () -> M) -> some View where A: View, M: View

 

struct AlertView: View {
    
    @State private var isShowing = false
    
    var body: some View {
        Button {
            isShowing.toggle()
        } label: {
            Text("Click Me!")
        }
        .alert("Alert Title", isPresented: $isShowing) {
            Button("확인", role: .none) {
                // 확인 버튼 눌렀을 때 처리할 코드
            }
            Button("취소", role: .cancel) {
                // 취소 버튼 눌렀을 때 처리할 코드
            }
        } message: {
            Text("Alert Message")
        }
    }
}

 

 

Confirmation Dialog

func confirmationDialog<A, M>(_ titleKey: LocalizedStringKey,
                                                       isPresented: Binding<Bool>,
                                                       titleVisibility: Visibility = .automatic, // hidden
                                                       @ViewBuilder actions: () -> A,
                                                       @ViewBuilder message: () -> M) -> some View where A: View, M: View

 

struct ConfirmationDialogView: View {
    
    @State private var isShowing = false
    
    var body: some View {
        Button {
            isShowing.toggle()
        } label: {
            Text("Click Me!")
        }
        .confirmationDialog("Confirmation Dialog Title", 
                            isPresented: $isShowing) {
            Button("사진 삭제", role: .destructive) {
                // 사진 삭제버튼 눌렀을 때 처리할 코드
            }
            Button("취소", role: .cancel) {
                // 취소버튼 눌렀을 때 처리할 코드
            }
        } message: {
            Text("이 사진이 모든 기기의 iCloud 사진에서 삭제됩니다.")
        }
    }
}

'iOS > SwiftUI' 카테고리의 다른 글

ProgressView  (0) 2023.11.21
TextField & SecureField  (0) 2023.11.21
Toggle  (2) 2023.11.20
새 화면 띄우기  (0) 2023.11.20
다크모드 색상지원  (3) 2023.11.19