macOS/iOS API解説

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

目次

buttons

INDEX>AppKit>NSAlert>

アラートウインドウのボタンを配列で返します

解説

アラートウインドウのボタンを配列で返します。
Swift

open var buttons: [NSButton] { get }

Objective-C

@property(readonly, copy) NSArray *buttons

返り値

Objective-C

( NSArray * )

Swift

 [NSButton] { get }

ボタンの配列

引数

フレームワーク

ApplicationKit

クラス

NSAlert

使用可能

10.3

編集時のバージョン

OS X 10.14.5
Swift 5.1

例文

Objective-C

NSLog([[alert buttons] description]);

Swift5.1

    //NSAlert buttons
    @IBAction func function015(sender: AnyObject) {
        print("function015 called")
        //End even if there are other modals
        NSApp.abortModal()
        
        //Create NSAlert
        let alert:NSAlert = NSAlert()
        
        let anApplication = NSApplication.shared
        let imageObj:NSImage = anApplication.applicationIconImage
        alert.icon = imageObj
        //1st button
        alert.addButton(withTitle: NSLocalizedString("Stop", comment:"停止"))
        //2nd button
        alert.addButton(withTitle: NSLocalizedString("Continue", comment:""))
        //Display sheet
        alert.beginSheetModal(for: self.view.window!) { responseCode in
            if NSApplication.ModalResponse.alertSecondButtonReturn == responseCode {
                print("press SecondButton")
                self.imageView.image = imageObj
            }
        }
        //Loop and check button
        for (index, button) in alert.buttons.enumerated() {
            print("button : index \(index) = \(button.title)")
        }
        
    }

Swift

    //NSAlert buttons
    @IBAction func function015(sender: AnyObject) {
        NSLog("function015 called")
        //なにかモーダルがあっても終わり
        NSApp.abortModal()
        
        //テキストの作成
        let messageText:String = "Message text" as String
        let informativeText:String = "Information text" as String
        //NSAlertの作成
        let alert:NSAlert = NSAlert()
        
        let anApplication = NSApplication.sharedApplication()
        var imageObj:NSImage = anApplication.applicationIconImage
        alert.icon = imageObj
        //1つめのボタン
        alert.addButtonWithTitle(NSLocalizedString("Stop", comment:"停止"))
        //2つめのボタン
        alert.addButtonWithTitle(NSLocalizedString("Continue", comment:""))
        //シートを出す
        alert.beginSheetModalForWindow(window) { responseCode in
            if NSAlertSecondButtonReturn == responseCode {
                NSLog("SecondButton")
                self.imageView.image = imageObj
            }
        }
        for (index, button) in enumerate(alert.buttons) {
            NSLog("SecondButton %d %@",index,(button as! NSButton).title ) //Swift 1.2
            //NSLog("SecondButton %d %@",index,(button as NSButton).title )
        }
        
    }