Search
⚠️

Alert 경고 메세지 띄우기

부제
카테고리
SwiftUI
세부 카테고리
컴포넌트
Combine 카테고리
최종편집일
2022/09/20 08:16
작성중
관련된 포스팅
생성 일시
2022/07/16 15:56
태그

Alert 기본 경고창 띄우기

기본 경고창에는 dismiss Button 만 존재
SwiftUI 에서는 이제 modifier 를 통해서 alert 를 띄운다. → 띄울 때마다 찾아볼 것.
아래의 파라미터를 준비해야한다
title : 경고창의 제목
isPresented : Binding<Bool> 타입 → true 시 경고창 보여줌
presenting data : 경고창의 소스가 되는 데이터
이를 이용해서 데이터와 관련된 동작을 추가하거나 데이터를 보여줄 수 있음
message : 경고창의 메세지
Presents an alert with a message using the given data to produce the alert’s content and a string variable as a title.
struct SaveDetails: Identifiable { let name: String let error: String } struct SaveView: View { var title: String @State var didError = false @State var details: SaveDetails? var body: some View { Button("Save File") { details = model.save(didError: $didError) } .alert( title, isPresented: $didError, presenting: details ) { detail in //Trailing 클로져 내부에는 구현하고자 하는 버튼을 구현한다. Button(role: .destructive) { // Handle delete action. } label: { Text(""" Delete \(detail.name) """) } Button("Retry") { // handle retry action. } } message: { detail in Text(detail.error) } } }
Swift
복사