[SwiftUI] Confirmation Dialog(=Action Sheet)

RudinP·2025년 6월 30일
0

Study

목록 보기
303/325

1. State var 추가

struct ConfirmationDialog: View {
	@State private var showDialog = false
}

2. confirmationDialog 모디파이어 추가

트리거되는 뷰에 추가하는 것이 보통이다.

title: 다이얼로그 제목
isPresented: 팝업 유무 관리 변수
titleVisibility: 제목을 어떻게 처리할지 결정. 기본값은 .automatic(파라미터 자체를 사용하지 않으면 기본으로 적용)

struct ConfirmationDialog: View {
	@State private var showDialog = false
    
    var body: some View {
    	Button {
        } label: {
        	Text("")
        }
        .confirmationDialog("컬러 선택", isPresented: $showDialog) {
        	Button("Red") {
            }
            
            Button("Green") {
            }
            
            Button("Blue") {
            }
            
            Button(role: .destructive) {
            } label: {
            	Text("Reset")
            }
            
            Button(role: .cancel) {
            } label: {
            	Text("취소")
            }
           
        } message: {
        	Text("다이얼로그에 표시할 메시지")
        }
    }
}

3. 트리거되는 곳에서 State var의 값 변경하는 코드 추가

toggle 혹은 = true 등으로 작업


데이터 전달

...
.confirmationDialog("컬러 선택", isPresented: $showDialog, presenting: ColorData.samples) { colors in
	ForEach(colors) { item in
    	Button(item.title) {
        	color = item.color
        }
    }
    
    ...
} message: { _ in
	...
}
profile
iOS 개발자가 되기 위한 스터디룸...

0개의 댓글