LoginSignup
13
13

More than 5 years have passed since last update.

Xamarin.Macにおけるシートいろいろ(NSAlert編)

Last updated at Posted at 2014-08-20

Problem

OSXのユーザーインタフェースガイドライン的にモーダルダイアログはよろしくないらしいので,できるだけシートを使っていくことになります。
シートを出すにもその目的や,やりたいことに応じていろいろあります。本稿ではNSAlertに着目し,次稿ではNSWindowに着目します。

with NSAlert

Notify

お手軽。Windowsで言うところのMessageBoxに相当します。
表示は簡単。使いたい場所で以下のように書きます。

simpleAlert
using (var alert = new NSAlert())
{
    alert.AlertStyle = NSAlertStyle.Informational;
    alert.MessageText = "タイトル";
    alert.InformativeText = "メッセージ";
    alert.RunSheetModal([NSWindow]);
}

[NSWindow]には紐付けるウィンドウを指定します。nullにすると通常のダイアログ表示になります。
Screen Shot 2014-08-20 at 13.06.05.png
Screen Shot 2014-08-20 at 13.07.39.png

Confirm

ボタンを表示して結果を受け取る方法。

simpleQuestion
using (var alert = new NSAlert())
{
    alert.AlertStyle = NSAlertStyle.Informational;
    alert.MessageText = "タイトル";
    alert.InformativeText = "聞きたいこと";
    alert.AddButton("はい");
    alert.AddButton("いいえ");
    var ret = alert.RunSheetModal([NSWindow]);
    return (ret == (int)NSAlertButtonReturn.First);
}

Screen Shot 2014-08-20 at 13.09.08.png

1アラートには3つまでボタンを設けることができ,どれが押されたかはNSAlertButtonReturn列挙で取得できます(First, Second, Third)。AddButtonするとNSAlertButtons[]に追加されるので,この順番になります。
追記(201408201515) 嘘です。結構好きな数追加できました。Thid以降は1004,1005...と返却されるintがインクリメントされていきます。

Suppress

あとよくあるのは,「二度と表示してくれるな」チェックボックスです。こんな感じです。

suppression
using (var alert = new NSAlert())
{
    alert.AlertStyle = NSAlertStyle.Informational;
    alert.MessageText = "お節介";
    alert.InformativeText = "お節介なメッセージ";
    alert.AddButton("はい");
    alert.AddButton("いいえ");
    alert.AddButton("キャンセル");
    alert.ShowsSuppressionButton = true;
    var ret = alert.RunSheetModal(Window);
    Debug.WriteLine(ret == (int)NSAlertButtonReturn.Third);
    Debug.WriteLine(alert.SuppressionButton.State);
}

表示終了後に,SuppressionButtonState (On/Off)で判定できます。
Screen Shot 2014-08-20 at 13.18.57.png
表示が英語なのはOSの言語設定の依るもので,これもただのNSButtonなので,変更できます。上のコードで表示前にalert.SuppressionButton.Title = "もういいよそういうのは";を追加するとこんな感じに。
Screen Shot 2014-08-20 at 13.43.48.png

Prompt

VBAとかでよくある,一行入力させるメッセージボックス的な。それを実現する方法。

prompt
var name = "default";
using (var alert = new NSAlert())
{
    alert.AlertStyle = NSAlertStyle.Informational;
    alert.MessageText = "おしえてくん";
    alert.InformativeText = "おなまえはなにかな?";
    alert.AddButton("確定");
    alert.AddButton("取消");

    var nameBox = new NSTextField(new RectangleF(0, 0, 200, 24));
    alert.AccessoryView = nameBox;

    var ret = alert.RunSheetModal(Window);
    Debug.WriteLine(ret == (int)NSAlertButtonReturn.First);
    name = nameBox.StringValue;
}
Debug.WriteLine(name);

Screen Shot 2014-08-20 at 13.28.31.png

NSAlert.AccessoryViewNSViewなので,入れようと思えば何でも入りますが,そこまでするならNSWindowをシート化したほうがいいです。アラートなので,ボタンを消すことはできませんし。

Conclusion

これでNSAlertはバッチリ。結果取得はBeginSheetForResponceを呼び出すことで非同期にできます。

13
13
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
13
13