0Pricing
SwiftUI Academy · レッスン

アクション付きアラート

アラートを表示し、ボタンタップに応答します。

「アクション付きアラート」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What Is an Alert?

An alert is a small pop-up that grabs attention with a title, a message, and one or more buttons the user must respond to. ⚠️

Alerts Use a Boolean

Like sheets, an alert shows when a boolean state turns true. Store that flag in your view and flip it to trigger the pop-up.

@State private var showAlert = false

The .alert Modifier

Attach .alert to a view, give it a title, and bind it to your boolean. SwiftUI presents the alert when the flag is true.

.alert("Saved!", isPresented: $showAlert) {
    Button("OK") { }
}

Adding a Button

Every alert needs at least one button. A plain OK button dismisses the alert and runs whatever code you put in its action.

Button("OK") { print("User confirmed") }

A Cancel Button

Mark a button with role .cancel to give it a clear way out. It appears bold and lets users back away safely.

Button("Cancel", role: .cancel) { }

A Destructive Button

The .destructive role turns a button red, signaling a risky action like deleting. Use it so users notice before they tap.

Button("Delete", role: .destructive) { remove() }

Adding a Message

Pass a second closure to .alert to show explanatory text under the title. Keep it short and clear so users decide quickly.

.alert("Delete file?", isPresented: $show) {
    Button("Delete", role: .destructive) { }
} message: {
    Text("This cannot be undone.")
}

Multiple Buttons

An alert can hold several buttons stacked together. SwiftUI arranges them and runs the matching action when one is tapped.

Button("Delete", role: .destructive) { }
Button("Cancel", role: .cancel) { }

Reacting to a Tap

Each button's action closure runs when tapped, then the alert closes automatically. Put your save, delete, or retry logic there.

Button("Retry") { reloadData() }

Alerts From an Error

Drive an alert from an optional error with .alert(_:isPresented:presenting:). The alert appears whenever the error value is non-nil.

Keep Alerts Rare

Alerts interrupt the user, so save them for important moments like errors or confirmations. Overusing them feels noisy and annoying.

Quick Check

Which button role turns an alert button red?

Recap: Alerts

You learned to show an alert from a boolean, add titles and messages, and use cancel and destructive button roles. Well done! 🎉

よくある質問

「アクション付きアラート」レッスンは無料ですか?

はい。「アクション付きアラート」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「アクション付きアラート」で何を学びますか?

アラートを表示し、ボタンタップに応答します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「アクション付きアラート」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. シートを表示する
  2. デタントとドラッグインジケーター
  3. アクション付きアラート
  4. 確認ダイアログ
← SwiftUI Academyに戻る