개발 창고/iOS

[SwiftUI] How to Use the Confirm Window

로이제로 2023. 11. 30. 22:00
반응형

 

1. 부분 소스

오늘은 SwiftUI에서 Confirm을 사용하는 방법에 대해 이야기해볼까 합니다.

.alert(isPresented: $isConfirm, content: {
    return Alert(
        title           : Text("확인 타이틀"),
        message         : Text("확인 메시지"),
        primaryButton   : .default(Text("확인"), action: {
            // 확인 액션
        }),
        secondaryButton: .destructive(Text("취소")))
            // 취소 액션
        })
attribute 내용
title Confirm의 제목으로 설정하지 않으면 그 영역만큼 표시 되지 않습니다.
message Confirm의 주요 내용이 보여집니다.
primaryButton 첫 번째 버튼입니다.
위 에서는 .default를 사용하여 Alter.Button.default의 기본 버튼을 사용하였습니다.
secondaryButton 두 번째 버튼입니다.
여기서는 .destructive를 사용하여 취소 버튼처럼 사용하였습니다.
.cancel로도 사용할 수 있지만, .cancel 보다 더 .destructive가 직관적이라 사용하였습니다.

 

2. 결과

TestView의 Show Confirm창

 

Show Confirm을 눌렀을 때 Confirm 창

 

3.테스트 전체 소스

import SwiftUI

// 테스트 페이지
struct TestView: View {
    @State var isConfirm = false
    
    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    self.isConfirm = true
                }){
                    Text("Show Confirm")
                }
            }
            .alert(isPresented: $isConfirm, content: {
                return Alert(
                    title           : Text("확인 타이틀"),
                    message         : Text("확인 메시지"),
                    primaryButton   : .default(Text("확인"), action: {
                        // 확인 액션
                    }),
                    secondaryButton: .destructive(Text("취소")))
                        // 취소 액션
                    })
        }
    }
}

// View 미리보기
struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

 

반응형