An object that displays an alert message to the user.
@MainActor class UIAlertController : UIViewController
Use this class to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present(_:animated:completion:)
method. UIKit displays alerts and action sheets modally over your app's content.
In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction(_:)
method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Listing 1 shows how to configure an alert with a single action.
addAction(_:)
메서드를 사용하여 추가하는 각 작업에 대해 알림 컨트롤러는 작업 세부정보가 포함된 버튼을 구성합니다. 사용자가 해당 작업을 탭하면 경고 컨트롤러는 작업 개체를 만들 때 제공한 블록을 실행합니다. 목록 1은 단일 작업으로 알림을 구성하는 방법을 보여줍니다.Listing 1 Configuring and presenting an 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)
When configuring an alert with the UIAlertController.Style.alert
style, you can also add text fields to the alert interface. The alert controller lets you provide a block for configuring your text fields prior to display. The alert controller maintains a reference to each text field so that you can access its value later.
UIAlertController.Style.alert
스타일로 알림을 구성할 때 알림 인터페이스에 텍스트 필드를 추가할 수도 있습니다. 경고 컨트롤러를 사용하면 표시하기 전에 텍스트 필드를 구성하기 위한 블록을 제공할 수 있습니다. 경고 컨트롤러는 나중에 해당 값에 액세스할 수 있도록 각 텍스트 필드에 대한 참조를 유지합니다.Important
The
UIAlertController
class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified
UIAlertController
클래스는 있는 그대로 사용하기 위한 것이며 하위 분류를 지원하지 않습니다. 이 클래스의 보기 계층 구조는 비공개이며 수정해서는 안 됩니다.Alerts
Getting the User's Attention with Alerts and Action Sheets
Present important information to the user or prompt the user about an important choice.