macOS/iOS API解説

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

目次

state

INDEX>AppKit>NSButton

ボタンの状態を返します

Objective-C

@property NSInteger state

Swift

var state: Int

解説

ボタンの状態を返します。
【ボタンの状態】
● NSOnState on
f:id:jjj777:20150228223430p:plain
● NSOffState off
f:id:jjj777:20150228223440p:plain
● NSMixedState Mixed
f:id:jjj777:20150228223449p:plain
ボタンがMixedの状態を使うには、allowsMixedState:メソッドを使います。

設定値

Objective-C

@property NSInteger state

Swift

var state: Int

フレームワーク

ApplicationKit

クラス

NSButton

使用可能

10.0

参照

例文

Objective-C

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
[myOutlet setAllowsMixedState:YES];


switch ( [myOutlet state] ){

    case NSMixedState:
        [myOutlet setTitle:@"mix"];
        break;
    
    case NSOffState:
        [myOutlet setTitle:@"off"];
        break;
        
    case NSOnState:
        [myOutlet setTitle:@"on"];
        break;
    }


}

@end

Swift

    //NSButton state
    @IBAction func function015(sender: AnyObject) {
        var aWindow : NSWindow
        = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300.0, 200.0),
            styleMask: NSTitledWindowMask
                | NSClosableWindowMask
                | NSMiniaturizableWindowMask
                | NSResizableWindowMask,
            backing: .Buffered,
            defer: false,
            screen: NSScreen.mainScreen())
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        aWindow.center()//ウインドウをスクリーンの中心に
        aWindow.title = "ウインドウタイトル"//タイトル設定
        //
        var theButton : NSButton = NSButton(frame: NSMakeRect(100.0, 90.0, 100.0, 30.0))
        theButton.title = "Change"
        theButton.bezelStyle = NSBezelStyle.RegularSquareBezelStyle
        theButton.setButtonType(NSButtonType.SwitchButton)
        theButton.action = Selector("buttonAction002:")
        theButton.target = self
        theButton.allowsMixedState = true
        theButton.state = NSMixedState
        //NSOnState
        //NSOffState
        aWindow.contentView.addSubview(theButton)
        
        aWindow.orderFront(self)//前面に
        aWindow.makeKeyAndOrderFront(self)//表示
        
    }