带操作的警告框
显示警告框并响应按钮点击。
带操作的警告框 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 = falseThe .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! 🎉
用 AI 导师学习 Swift — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「带操作的警告框」课时是免费的吗?
是的 — 「带操作的警告框」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「带操作的警告框」这节课中我会学到什么?
显示警告框并响应按钮点击。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「带操作的警告框」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。