SwiftUI 개발 중 최소타겟 iOS14버전으로 하는 중 alert이 필요한 상황이 있었다.
아래와 같이 .alert 코드 작성 시 아래에 있는 .alert만 호출이 된다.
 .alert(isPresented: $presentAlert) { 
            Alert(
                title: Text("Title"),
                message: Text("Message")
            )
        }
 .alert(isPresented: $presentAlert2) { 
            Alert(
                title: Text("Title2"),
                message: Text("Message2")
            )
        }그래서 검색해보니 스택오버플로우에서 활용방안에 대해서 나와있었다.
https://stackoverflow.com/questions/58069516/how-can-i-have-two-alerts-on-one-view-in-swiftui
위의 있는 예시 코드를 바꿔보자면
enum loginResultAlert {
        case first, second
    }
    .alert(isPresented: $presentAlert) {
                switch loginResultAlert {
                case .first:
                    let alert = Alert(
                        title: Text("Title2"),
                        message: Text("Message2")
                    )
                    return alert
                case .second:
                    let alert = Alert(
                        title: Text("Title2"),
                        message: Text("Message2")
                    )
                    return alert
                }
            }각 enum으로 설정한 값으로 들어올시 alert이 변하는 것을 확인할 수 있었다.
'iOS > SwiftUI' 카테고리의 다른 글
| SwiftUI TextField (0) | 2023.11.10 | 
|---|---|
| SwiftUI ZStack 활용 (0) | 2023.11.07 | 
| SwiftUI) NavigationBar 뒤로가기 버튼 안보이게 하기 (0) | 2023.06.12 | 
| SwiftUI) List NavigationLink item ">" 숨기는 방법 (0) | 2023.05.31 | 
| @StateObject vs @ObservedObject 차이점 (0) | 2023.01.07 |