Present important information to the user or prompt the user about an important choice.
Display an alert or action sheet when your app requires additional information or acknowledgment from the user. Alerts and action sheets interrupt your app’s normal flow to display a message to the user. In Figure 1, the left image shows an alert and the right image shows an action sheet. The user dismisses an alert or action sheet by selecting one of the listed options.
Figure 1 Alerts and action sheets
Important
Because alerts and action sheets are an interruption, use them sparingly and only when absolutely needed. For detailed guidance on when to use them, see iOS Human Interface Guidelines.
To display an alert or action sheet, create a UIAlertController
object, configure it, and call its present(_:animated:completion:)
method, as shown in Listing 1. Configuring the alert controller includes specifying the title and message that you want the user to see and the actions the user can select. You must add at least one action—represented by a UIAlertAction
object—to an alert controller before presenting it.
UIAlertController
개체를 만들고 구성한 다음 현재(_:animated:completion:)
메서드를 호출합니다. 사용자가 볼 수 있고 사용자가 선택할 수 있는 작업. 경고 컨트롤러를 표시하기 전에 UIAlertAction
개체로 표시되는 작업을 하나 이상 추가해야 합니다.@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
}
}
On iPad, UIKit requires that you display an action sheet inside a popover. Figure 2 shows an action sheet anchored to a bar button item.
Figure 2 Action sheets in popovers