Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - SwiftUI] Alert, ActionSheet (.alert, .confirmationDialog) 사용 방법 본문

iOS 기본 (SwiftUI)

[iOS - SwiftUI] Alert, ActionSheet (.alert, .confirmationDialog) 사용 방법

jake-kim 2022. 9. 1. 22:53

목차) SwiftUI의 기본 - 목차 링크

Alert

https://developer.apple.com/documentation/swiftui/alert

  • iOS 13~iOS 15까지는 위 Alert를 사용했지만, deprecated
    • iOS 15부터는 .alert 사용을 권장
    • .alert는 더욱 선언적으로 alert를 사용할 수 있는 방법
  • 위 Alert를 하나의 View로 존재했지만, iOS15부터는 메소드로 사용이 가능 (더욱 선언적 프로그래밍)

https://developer.apple.com/documentation/swiftui/view/alert(_:ispresented:presenting:actions:message:)-8584l

  • 사용 방법은 Button에 .alert하여 추가
    • alert가 보이는 상태 변수도 하나 추가
    • .alert 메소드에는 titleKey, isPresented, View 순서대로 주입하여 사용
@State var isShowing = false

var body: some View {
  Button("Alert") {
    isShowing = true
  }
  .alert("title", isPresented: $isShowing) {
    Button("OK", role: .destructive) { print("tap ok") }
    Button("cancel", role: .cancel) { print("tap cancel") }
  }
}

.alert 사용 화면

ActionSheet

https://developer.apple.com/documentation/swiftui/actionsheet

  • ActionSheet도 Alert와 동일하게 iOS 16까지만 지원
    • Alert와 동일하게 별도의 뷰 인스턴스로 ActionSheet를 사용하는 것보단 메소드로 접근하여 사용하는게 더욱 선언적 프로그래밍이라서 deprecated
  • 메소드로 ActionSheet 사용

https://developer.apple.com/documentation/swiftui/view/confirmationdialog(_:ispresented:titlevisibility:presenting:actions:message:)-8y541

  • 사용 방법은 .alert 사용방법과 완전 동일하고 메소드 이름만 .alert에서 .confirmationDialog로 다르게 사용
@State var isActionSheetShowing = false

Button("ActionSheet") {
  isActionSheetShowing = true
}
.confirmationDialog("title", isPresented: $isActionSheetShowing) {
  Button("OK", role: .destructive) { print("tap ok") }
  Button("cancel", role: .cancel) { print("tap cancel") }
}

* 전체 코드: https://github.com/JK0369/ExActionSheet

 

* 참고

https://developer.apple.com/documentation/swiftui/view/confirmationdialog(_:ispresented:titlevisibility:presenting:actions:message:)-8y541 

https://developer.apple.com/documentation/swiftui/actionsheet

https://developer.apple.com/documentation/swiftui/view/alert(_:ispresented:presenting:actions:message:)-8584l 

https://developer.apple.com/documentation/swiftui/alert

 

Comments