macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

toolbar

INDEX>AppKit> NSWindow

ウインドウのツールバー

Objective-C

@property(strong) NSToolbar *toolbar

Swift

var toolbar: NSToolbar?

解説

ウインドウのツールバー

返り値

ツールバーオブジェクト
Objective-C

NSToolbar

Swift

NSToolbar?

フレームワーク

ApplicationKit

クラス

NSWindow

使用可能

10.1

参照

- setToolbar:

更新時のバージョン

OS X 10.10

関連記事(外部サイト)

developer.apple.com

例文

#import "Controller.h"

@implementation Controller

- (IBAction)pushButton:(id)sender
{

NSToolbar *toolbar1 = [[[NSToolbar alloc] initWithIdentifier: @"DocTool"] autorelease];
NSToolbar *toolbar2;
[myWindow setToolbar: toolbar1];

toolbar2 =   [myWindow toolbar]; 
}

@end

Swift

    //NSWindow toolbar
    func buttonAction083(sender: AnyObject){
        var aWindow : NSWindow = (sender as NSButton).window!
        aWindow.toggleToolbarShown(self)
    }
    func toolBarAction083(sender: AnyObject){
        NSLog("!!!")
    }
    
    //NSToolbarDelegate にはこの3つのメソッドが必要
    var toolBarArray : [String] = ["toolBarItem1",NSToolbarSeparatorItemIdentifier,"SearchDocToolbarItemIdentifier"]
    func toolbarAllowedItemIdentifiers(toolbar: NSToolbar) -> [AnyObject] {
        return toolBarArray
    }
    func toolbarDefaultItemIdentifiers(toolbar: NSToolbar) -> [AnyObject] {
        return toolBarArray
    }
    func toolbar(toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: String, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        var aToolBarItem : NSToolbarItem = NSToolbarItem()
        if ( itemIdentifier == "SearchDocToolbarItemIdentifier"){
            
            aToolBarItem  = NSToolbarItem(itemIdentifier: "SearchDocToolbarItemIdentifier")
            aToolBarItem.label = "Search"
            aToolBarItem.paletteLabel = "Search"
            aToolBarItem.toolTip = "Search item"

            //検索
            let searchFieldOutlet : NSSearchField = NSSearchField(frame: NSMakeRect(0.0, 0.0, 100.0,30.0))
            aToolBarItem.view = searchFieldOutlet
            

        }else if( itemIdentifier == "toolBarItem1" ){
            aToolBarItem = NSToolbarItem(itemIdentifier: "toolBarItem1")
            aToolBarItem.label = "item 1"
            aToolBarItem.paletteLabel = "item 1"
            aToolBarItem.toolTip = "toolbar item 1 "
            aToolBarItem.image = NSImage(named: "toolbarImage")
            aToolBarItem.action = Selector("toolBarAction083:")
            aToolBarItem.target = self 
        }else{
            
        }
        return aToolBarItem
    }

    @IBAction func function083(sender: AnyObject) {
        var theWindow : NSWindow = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300, 200), styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing: .Buffered, defer: false)
        windowArray.addObject(theWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        
        //ボタンを作成
        let theButton : NSButton = NSButton(frame: NSMakeRect(100.0, 2.0, 100.0, 30.0))
        theButton.title = "Toolbar"
        theButton.bezelStyle = NSBezelStyle.RoundedBezelStyle
        theButton.action = Selector("buttonAction083:")
        theButton.target = self
        theWindow.contentView.addSubview(theButton)
        
        //ツールバーを作成
        let theToolBar : MyToolbar083 = MyToolbar083(identifier: "mainToolBar")
        theToolBar.delegate = self 
        
        //ツールバーの表示モード
        theToolBar.displayMode = NSToolbarDisplayMode.IconAndLabel //アイコントラベル
        //theToolBar.displayMode = NSToolbarDisplayMode.IconOnly //アイコンのみ
        
        //theToolBar.insertItemWithItemIdentifier("toolBarItem1", atIndex: 0)
        
        theWindow.toolbar = theToolBar
        //ウインドウの表示
        theWindow.center()//ウインドウをスクリーンの中心に
        theWindow.title = "ウインドウタイトル"//タイトル設定
        theWindow.orderFront(self)//前面に
        theWindow.makeKeyAndOrderFront(self)//表示
        
        
    }