LoginSignup
5
3

More than 3 years have passed since last update.

[macOS][Cocoa]アラートの表示

Last updated at Posted at 2020-02-03

アラートの表示

ボタンを追加するとか、ショートカットキーを設定するとか調べたのでメモ。

alert.png

ソース

let alert = NSAlert()

// 表示メッセージとインフォメーション
alert.messageText = "めっせーじ"
alert.informativeText = "いんふぉめーしょん"

// メッセージのスタイル
alert.alertStyle = .informational

// ボタン追加
alert.addButton(withTitle: "1つめ")
alert.addButton(withTitle: "2つめ")

// ボタンに対してショートカットキーの設定する場合
let buttons = alert.buttons
buttons[0].keyEquivalent = "\u{1b}"    // esc key
// buttons[0].tag = 1234               // runModalの戻り値を変更する場合
buttons[1].keyEquivalent = "\r"        // enter key

// 表示
let ret = alert.runModal()

switch ret {
case .alertFirstButtonReturn:
    print("1つめのボタン押されたよ。")
case .alertSecondButtonReturn:
    print("2つめのボタン押されたよ。")
default:
    print("その他:\(ret)")
}

補足

ボタンのショートカットキー

ショートカットキーを何も設定しない場合、デフォルトで最初のボタンはエンターキー、「Cancel」タイトルのボタンにはエスケープキー、「Don't Save」タイトルのボタンはCommand-D(最初のボタンではない場合のみ)に設定されます。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3