The key for the localized string that describes the title of the alert.
alert(_:isPresented:actions:)
Availability 有効性
- iOS 15.0+
- iPadOS 15.0+
- macOS 12.0+
- Mac Catalyst 15.0+
- tvOS 15.0+
- watchOS 8.0+
Technology
- Swift
UI
Declaration 宣言
func alert<A>(_ titleKey: LocalizedStringKey
, isPresented: Binding
<Bool
>, actions: () -> A) -> some View
where A : View
Parameters パラメータ
titleKey
isPresented
A binding to a Boolean value that determines whether to present the alert. When the user presses or taps one of the alert’s actions, the system sets this value to
false
and dismisses.actions
A
View
returning the alert’s actions.Builder
Discussion 議論
In the example below, a button conditionally presents an alert depending upon the value of a bound Boolean variable. When the Boolean value is set to true
, the system displays an alert with an “OK” action.
struct LoginView: View {
var didFail: Bool
var body: some View {
LoginForm(didFail: $didFail)
.alert("An error occurred.", isPresented: $didFail) {
Button("OK") {
// Handle acknowledgement.
}
}
}
}
All actions in an alert dismiss the alert after the action runs. The default button is shown with greater prominence. You can influence the default button by assigning it the default
keyboard shortcut.
The system may reorder the buttons based on their role and prominence.
If no actions are present, the system includes a standard “OK” action. No default cancel action is provided. If you want to show a cancel action, use a button with a role of cancel
.
On iOS, tvOS, and watchOS, alerts only support controls with labels that are Text
. Passing any other type of view results in the content being omitted.
This modifier creates a Text
view for the title on your behalf, and treats the localized key similar to init(_:
. See Text
for more information about localizing strings.
Text
を文字列のローカライズについてのさらなる情報として見てください。