alert
Presents an alert when a given condition is true, using a localized string resource for the title.
Overloads
12 ways to call compose().alert(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().alert(
titleResource: string,
options: {
isPresented: Binding<boolean>;
actions: ReactNode;
}
)@export(implementation) nonisolated func alert<A>(
_ titleResource: LocalizedStringResource,
isPresented: Binding<Bool>,
@ContentBuilder actions: () -> A
) -> some View
where A : View3 more Swift overloads share this form
nonisolated func alert<A>(
_ titleKey: LocalizedStringKey,
isPresented: Binding<Bool>,
@ContentBuilder actions: () -> A
) -> some View
where A : Viewnonisolated func alert<A>(
_ title: Text,
isPresented: Binding<Bool>,
@ContentBuilder actions: () -> A
) -> some View
where A : Viewnonisolated func alert<S, A>(
_ title: S,
isPresented: Binding<Bool>,
@ContentBuilder actions: () -> A
) -> some View
where S : StringProtocol, A : ViewParameters
titleResource*stringText resource for the localized string that describes the title of the alert.
options.isPresented*Binding<boolean>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
falseand dismisses.options.actions*ReactNodeA
ContentBuilderreturning the alert's actions.
Discussion
In the example below, a login form conditionally presents an alert by
setting the didFail state variable. When the form sets the value to
to true, the system displays an alert with an "OK" action.
struct Login: View {
@State private var didFail = false
var body: some View {
LoginForm(didFail: $didFail)
.alert(
"Login failed.",
isPresented: $didFail
) {
Button("OK") {
// Handle the 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
KeyboardShortcut.defaultAction 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 ButtonRole.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. See
Text for more information about localizing strings.
See also
More in Presentation.
- confirmationDialogPresents a confirmation dialog when a given condition is true, using a localized string resource for the title.
- contextMenuAdds an item-based context menu to a view.
- dialogIconConfigures the icon used by dialogs within this view.
- dialogSuppressionToggleEnables user suppression of dialogs and alerts presented within self, with a custom suppression message on macOS. Unused on other platforms.
- dismissalConfirmationDialogPresents a confirmation dialog upon an attempt to dismiss the window for this view.
- documentBrowserContextMenuAdds to a DocumentLaunchView actions that accept a list of selected files as their parameter.
- fileDialogBrowserOptionsOn macOS, configures the fileExporter, fileImporter, or fileMover to provide a refined URL search experience: include or exclude hidden files, allow searching by tag, etc.
- fileDialogConfirmationLabelOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom confirmation button label.
- fileDialogCustomizationIDOn macOS, configures the fileExporter, fileImporter, or fileMover to persist and restore the file dialog configuration.
- fileDialogDefaultDirectoryConfigures the fileExporter, fileImporter, or fileMover to open with the specified default directory.
- fileDialogImportsUnresolvedAliasesOn macOS, configures the fileExporter, fileImporter, or fileMover behavior when a user chooses an alias.
- fileDialogMessageOn macOS, configures the fileExporter, fileImporter, or fileMover with a custom message that is presented to the user, similar to a title.