UIAlertController

Groot·2022년 8월 24일
0

TIL

목록 보기
49/148
post-thumbnail

TIL

🌱 난 오늘 무엇을 공부했을까?

📌 UIAlertController - 공식문서

  • 사용자에게 Alert 메시지를 표시하는 개체입니다.

📍 Declaration

@MainActor class UIAlertController : UIViewController

📍 Overview

  • 이 클래스를 사용하여 표시할 메시지와 선택할 작업이 포함된 Alert 및 Action Sheet를 구성합니다.
  • 원하는 액션과 스타일로 Alert Controller를 구성한 후, present(_:animated:completion:) 메서드를 사용하여 표시합니다.
  • UIKit은 앱 콘텐츠 위에 Alert 및 Action Sheet를 Modal로 표시합니다.
  • 사용자에게 메시지를 표시하는 것 외에도 Alert Controller와 작업을 연결하여 사용자가 응답할 수 있는 방법을 제공할 수 있습니다.
  • addAction(_:) 메서드를 사용하여 추가하는 각 작업에 대해 Alert Controller는 작업 세부 정보가 있는 버튼을 구성합니다.
  • 사용자가 해당 작업을 탭하면 Alert Controller는 작업 개체를 만들 때 제공한 블록을 실행합니다.
  • 아래 코드는 단일 작업으로 Alert를 구성하는 방법을 보여줍니다.
    let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert) 
    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in 
    NSLog("The \"OK\" alert occured.")
    }))
    self.present(alert, animated: true, completion: nil)
  • UIAlertController.Style.alert 스타일로 Alert를 구성할 때 Alert 인터페이스에 텍스트 필드를 추가할 수도 있습니다.
  • Alert Controller를 사용하면 표시하기 전에 텍스트 필드를 구성하기 위한 블록을 제공할 수 있습니다.
  • Alert Controller는 나중에 해당 값에 액세스할 수 있도록 각 텍스트 필드에 대한 참조를 유지합니다.

    UIAlertController 클래스는 있는 그대로 사용하기 위한 것이며 서브클래싱을 지원하지 않습니다.
    이 클래스의 보기 계층 구조는 비공개이며 수정해서는 안 됩니다.

UIAlertController - 공식문서

📌 Getting the User's Attention with Alerts and Action Sheets

  • 사용자에게 중요한 정보를 제공하거나 중요한 선택에 대해 사용자에게 프롬프트합니다.

📍 Overview

  • 앱에서 추가 정보나 사용자 승인이 필요한 경우 Alert 또는 Action Sheet를 표시합니다.

  • Alert 및 Action Sheet는 앱의 정상적인 흐름을 중단하여 사용자에게 메시지를 표시합니다.

  • 아래에서 왼쪽 이미지는 Alert를 나타내고 오른쪽 이미지는 action sheet를 보여줍니다.

  • 사용자는 나열된 옵션 중 하나를 선택하여 Alert 또는 Action Sheet를 닫습니다.

    Alert 및 Action Sheet는 interruption이므로 꼭 필요한 경우에만 드물게 사용하십시오.
    언제 사용해야 하는지에 대한 자세한 지침은 iOS Human Interface Guidelines을 참조하십시오.

  • Alert 또는 Action Sheet를 표시하려면 아래와 같이 UIAlertController 개체를 만들고 구성한 다음 present(_:animated:completion:) 메서드를 호출합니다.

  • Alert 컨트롤러 구성에는 사용자에게 표시할 제목 및 메시지 지정 및 사용자가 선택할 수 있는 작업이 포함됩니다.

  • Alert 컨트롤러를 표시하기 전에 UIAlertAction 개체로 표시되는 하나 이상의 작업을 Alert 컨트롤러에 추가해야 합니다.

@IBAction func agreeToTerms() {
   // Create the action buttons for the alert.
   let defaultAction = UIAlertAction(title: "Agree", 
                        style: .default) { (action) in
	// Respond to user selection of the action.
   }
   let cancelAction = UIAlertAction(title: "Disagree", 
                        style: .cancel) { (action) in
	// Respond to user selection of the action.
   }
   
   // Create and configure the alert controller.     
   let alert = UIAlertController(title: "Terms and Conditions",
         message: "Click Agree to accept the terms and conditions.",
         preferredStyle: .alert)
   alert.addAction(defaultAction)
   alert.addAction(cancelAction)
        
   self.present(alert, animated: true) {
      // The alert was presented
   }
}
profile
I Am Groot

0개의 댓글