macOS/iOS API解説

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

目次

setButtonType:

INDEX>AppKit>NSButton

ボタンタイプをセットします

Objective-C

- (void)setButtonType:(NSButtonType)aType

Swift

func setButtonType(_ aType: NSButtonType)

解説

ボタンタイプをセットします。

【NSButtontype】
● NSMomentaryLight
● NSMomentaryPushButton
● NSMomentaryChangeButton
● NSPushOnPushOffButton on/offボタン
● NSOnOffButton
● NSToggleButton
● NSSwitchButton チェックボックス
● NSRadioButton ラジオボタン


chatgpt.com

返り値

なし

引数

Objective-C

(NSButtonType)aType

Swift

_ aType: NSButtonType

ボタンタイプの定数

フレームワーク

ApplicationKit

クラス

NSButton

使用可能

10.0-

関連記事(外部サイト)

例文

Objective-C

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
	//senderはボタン
	[sender setButtonType : NSToggleButton ];
}

@end

Swift

//NSButton setButtonType
    //Swift2.0
    @IBAction func function001(sender: AnyObject) {
        myButton.setButtonType(NSButtonType.PushOnPushOffButton)
        // MomentaryLightButton // was NSMomentaryPushButton
        // PushOnPushOffButton
        // ToggleButton
        // SwitchButton
        // RadioButton
        // MomentaryChangeButton
        // OnOffButton
        // MomentaryPushInButton // was NSMomentaryLight
    }


Swift UI

//
//  ContentView.swift
//  setButtonType_
//
//  Created  on 2025/02/24.
//  NSButton ButtonType

import SwiftUI
import AppKit

//カスタムボタンを作成
struct CustomButton: NSViewRepresentable {
    let title: String
    let type: NSButton.ButtonType
    let action: () -> Void

    func makeNSView(context: Context) -> NSButton {
        let button = NSButton(title: title, target: context.coordinator, action: #selector(Coordinator.buttonClicked))
        button.setButtonType(type)
        return button
    }

    func updateNSView(_ nsView: NSButton, context: Context) {
        nsView.title = title
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(action: action)
    }

    class Coordinator: NSObject {
        let action: () -> Void

        init(action: @escaping () -> Void) {
            self.action = action
        }

        @objc func buttonClicked() {
            action()
        }
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
            CustomButton(title: "カスタムボタン", type: .momentaryPushIn) {
                            print("ボタンが押されました")
                        }
            .frame(width: 200)
                    
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

更新時のバージョン

macOS 15.3.1