Alert 提醒(Alert) macos

Empty the Trash?

You can’t undo this action.

标本可交互 —— 点点看。Specimen is live — try it.

Anatomy — every part, named解剖 —— 每个部件的名字

  1. 1 Badged app icon带角标的 App 图标(Badged app icon) NSAlert.icon

    “The yellow triangle over the app icon” is the alert icon — your app's icon, badged with the caution symbol when alertStyle is .warning or .critical.

    “App 图标上的黄色三角”就是提醒图标 —— 你的 App 图标,当 alertStyle 是 .warning 或 .critical 时叠上警示角标。

    Prompt fragmentPrompt 片段

    the NSAlert icon — the app icon badged with the yellow caution triangle (NSAlert.icon, alertStyle .warning)

    NSAlert 的图标 —— 带黄色警示三角角标的 App 图标(NSAlert.icon,alertStyle .warning)

  2. 2 Message text消息文本(Message text) NSAlert.messageText

    “The bold line” is messageText — the one-sentence summary, phrased as a question when the alert asks for a decision.

    “那行粗体字”就是 messageText —— 一句话摘要;当提醒要求做决定时,措辞写成一个问题。

    Prompt fragmentPrompt 片段

    the bold headline of an NSAlert (NSAlert.messageText)

    NSAlert 的粗体标题行(NSAlert.messageText)

  3. 3 Informative text说明文本(Informative text) NSAlert.informativeText

    “The smaller gray text under the bold line” is informativeText — the consequences, spelled out in a sentence.

    “粗体行下面的灰色小字”就是 informativeText —— 用一句话把后果讲清楚。

    Prompt fragmentPrompt 片段

    the smaller explanation line of an NSAlert (NSAlert.informativeText)

    NSAlert 的较小解释文字(NSAlert.informativeText)

  4. 4 Suppression checkbox“不再询问”复选框(Suppression checkbox) NSAlert.showsSuppressionButton

    “The don't ask me again checkbox” is the suppression button — enable it with showsSuppressionButton and read suppressionButton.state after the alert returns.

    “别再问我”那个复选框就是 suppression button —— 用 showsSuppressionButton 开启,提醒返回后读取 suppressionButton.state。

    Prompt fragmentPrompt 片段

    the “Don't ask me again” checkbox of an NSAlert (showsSuppressionButton = true, read suppressionButton.state)

    NSAlert 的“不再询问”复选框(showsSuppressionButton = true,读取 suppressionButton.state)

  5. 5 Default button默认按钮(Default button) NSAlert.addButton(withTitle:)

    “The blue button” is the default button — the first button added to the alert; it gets the accent color and the Return key.

    “那个蓝色按钮”就是默认按钮 —— 添加到提醒上的第一个按钮;它获得强调色,也响应回车键。

    Prompt fragmentPrompt 片段

    the blue default button of an NSAlert — the first addButton(withTitle:), key equivalent Return

    NSAlert 的蓝色默认按钮 —— 第一个 addButton(withTitle:),键等价是回车

Prompt — paste into your agentPrompt —— 直接粘贴给你的代理

Show a native macOS alert with NSAlert (SwiftUI: .alert): messageText is the bold summary line, informativeText the smaller explanation. Add buttons with addButton(withTitle:) — the first one becomes the blue default button and answers the Return key; a button titled Cancel answers Escape. Set showsSuppressionButton = true for the “Don't ask me again” checkbox and read suppressionButton.state afterwards.

用 NSAlert 显示一个原生 macOS 提醒(SwiftUI:.alert):messageText 是那行粗体摘要,informativeText 是较小的解释文字。用 addButton(withTitle:) 添加按钮 —— 第一个会成为蓝色默认按钮,响应回车键;标题为 Cancel 的按钮响应 Escape。设置 showsSuppressionButton = true 显示“不再询问”复选框,结束后读取 suppressionButton.state。

Debug prompt — when it misbehaves调试 Prompt —— 当它不听话时

Debug my macOS alert (NSAlert, SwiftUI View.alert). Rule out: presenting off the main thread; runModal blocking the whole app when beginSheetModal(for:) attached to one window was intended; the return-key default being the FIRST button added, not the one you expect; the suppression checkbox state never persisted to defaults. The symptom:

调试我的 macOS 提醒(NSAlert、SwiftUI View.alert)。逐项排除:没在主线程上弹出;本想用 beginSheetModal(for:) 附着到单个窗口,runModal 却把整个 App 卡住;回车默认按钮是添加的第一个按钮,而不是你以为的那个;抑制复选框的状态从没写入 defaults 持久化。症状是:

In code代码里叫什么

Framework框架Symbol符号Note说明
AppKit NSAlert
SwiftUI .alert(_:isPresented:actions:message:)
AppKit NSAlert.messageText
AppKit NSAlert.informativeText
AppKit NSAlert.showsSuppressionButton
AppKit NSAlert.addButton(withTitle:)
AppKit NSAlert.alertStyle .warning / .informational / .critical.warning / .informational / .critical
AppKit NSAlert.beginSheetModal(for:completionHandler:) attach to one window instead改为附着到单个窗口

See also相关词条