更新履歴
広告
PRサイト
関連サイト

ツールバーを初期化する


辞書を生成し、解放する

ToolbarControllerの初期化メソッドで辞書を生成し、deallocメソッドで解放します。

ToolbarController.m
- (id)init
{
    self = [super init];
    if(self)
    {
        toolbarItems =
            [[NSMutableDictionary alloc] initWithCapacity:6];
    }
    return self;
}

- (void)dealloc
{
    [toolbarItems release];
    [super dealloc];
}


NSToolbarItemとNSToolbarを生成する

NSToolbarItemを設定するのにアウトレットが必要なので、NSToolbarItemの生成はawakeFromNibで行ないます。各NSToolbarItemについて以下の流れで処理しています。

  • 各NSToolbarItemをallocする
  • 必要な設定を行なう
  • 辞書に登録する
  • releaseする

辞書に登録した時点で辞書が各オブジェクトの所有権を持つので、その後直ちにreleaseして所有権を放棄しても大丈夫です。

設定の際、identifierと辞書登録時のキーを一致させている点に注意して下さい。こうしておくと要求されたidentifierに対応するNSToolbarItemを返すのが楽になります。

NSToolbarItemには二つのタイプがあります。ボタンのような働きをするタイプと、指定されたビューを持つタイプです。どちらにも共通の設定はラベルの設定です。このラベルはローカライズする必要があるので、直接文字列を指定せずNSLocalizedString()関数を使います。Localizable.stringsファイルにはNSLocalizedString()関数で指定したキーに対応する文字列を、それぞれの言語毎に用意します。

ボタンのような働きをするタイプにはボタンを表す絵とターゲット、アクションをsetImage:、setTarget:、setAction:メソッドで指定します。ターゲットは全てウィンドウコントローラにしてあるので、ウィンドウコントローラ側でこのメソッドを実装しておく必要があります。

指定されたビューを持つタイプの場合はsetView:でビューをセットし、setMinSize:、setMaxSize:メソッドで最小・最大サイズを指定します。最小・最大サイズ共ビュー自身のサイズにしてあるので、サイズは変化しません。テキストフィールドのvalueをドキュメントにバインドさせているので、ビューに入力された数値は自動的に反映されます。

ToolbarController.m
- (void)awakeFromNib
{
    NSToolbarItem   *anItem;
    NSToolbar       *theBar;
    
    anItem = [[NSToolbarItem alloc] initWithItemIdentifier:
        @"randomize"];
    [anItem setLabel:NSLocalizedString(@"randomizeLabel",nil)];
    [anItem setImage:[NSImage imageNamed:@"randomize.png"]];
    [anItem setTarget:windowController];
    [anItem setAction:@selector(randomizeRepeatingMotif:)];
    [toolbarItems setObject:anItem forKey:@"randomize"];
    [anItem release];
    
    anItem = [[NSToolbarItem alloc] initWithItemIdentifier:
        @"export"];
    [anItem setLabel:NSLocalizedString(@"exportLabel",nil)];
    [anItem setImage:[NSImage imageNamed:@"export.png"]];
    [anItem setTarget:windowController];
    [anItem setAction:@selector(export:)];
    [toolbarItems setObject:anItem forKey:@"export"];
    [anItem release];
    
    anItem = [[NSToolbarItem alloc] initWithItemIdentifier:
        @"pictureWidth"];
    [anItem setLabel:NSLocalizedString(@"pictureWidthLabel",nil)];
    [anItem setView:pictureWidthView];
    [anItem setMinSize:[pictureWidthView bounds].size];
    [anItem setMaxSize:[pictureWidthView bounds].size];
    [toolbarItems setObject:anItem forKey:@"pictureWidth"];
    [anItem release];
    
    anItem = [[NSToolbarItem alloc] initWithItemIdentifier:
        @"pictureHeight"];
    [anItem setLabel:NSLocalizedString(@"pictureHeightLabel",nil)];
    [anItem setView:pictureHeightView];
    [anItem setMinSize:[pictureHeightView bounds].size];
    [anItem setMaxSize:[pictureHeightView bounds].size];
    [toolbarItems setObject:anItem forKey:@"pictureHeight"];
    [anItem release];
    
    anItem = [[NSToolbarItem alloc] initWithItemIdentifier:
        @"editor"];
    [anItem setLabel:NSLocalizedString(@"editorLabel",nil)];
    [anItem setImage:[NSImage imageNamed:@"editor.png"]];
    [anItem setTarget:windowController];
    [anItem setAction:@selector(toggleEditorDrawer:)];
    [toolbarItems setObject:anItem forKey:@"editor"];
    [anItem release];

最後にNSToolbarを生成します。初期化時にidentifierをRMGToolbarとしていますが、この文字列は特に使用していません。

ツールバーのデリゲートをToolbarControllerに設定して、メッセージをToolbarControllerで受ける様にします。その後ウィンドウにツールバーをセットしてから、ツールバーをreleaseしておしまいです。

    theBar = [[NSToolbar alloc] initWithIdentifier:@"RMGToolbar"];
    [theBar setDelegate:self];
    [[windowController window] setToolbar:theBar];
    [theBar release];
}

Localizable.strings(English)
"randomizeLabel" = "Randomize";
"exportLabel" = "Export…";
"pictureWidthLabel" = "Image Width";
"pictureHeightLabel" = "Image Height";
"editorLabel" = "Editor";

Localizable.strings(Japanese)
"randomizeLabel" = "ランダム";
"exportLabel" = "書き出し…";
"pictureWidthLabel" = "画像の幅";
"pictureHeightLabel" = "画像の高さ";
"editorLabel" = "エディタ";

Localizable.strings(zh_TW)
"randomizeLabel" = "隨機變化";
"exportLabel" = "輸出…";
"pictureWidthLabel" = "圖片寬度";
"pictureHeightLabel" = "圖片高度";
"editorLabel" = "編輯器";


ページの先頭へ戻る