macOS/iOS API解説

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

目次

addButtonWithTitle:

INDEX>AppKit>NSAlert>

アラートウインドウにボタンを追加します

swift

open func addButton(withTitle title: String) -> NSButton

objective-C

-(NSButton *)addButtonWithTitle:(NSString *)aTitle:

解説

アラートウインドウにボタンを追加します。
左から右へと書く言語の場合、ボタンは右から順に1,2、3番目となります。

f:id:jjj777:20150208224407p:plain

返り値

NSButton
( NSButton * )

追加したボタン

引数

NSButton
( NSString * )aTitle

追加するボタンのタイトル

フレームワーク

ApplicationKit

クラス

NSAlert

使用可能

10.3

編集時のバージョン

OS X 10.15 swift5.1

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
//アラートパネルを作る
NSAlert *alert = [NSAlert alertWithMessageText:@"alertWithMessageText"
                defaultButton:@"defaultButton"
                alternateButton:@"alternateButton"
                otherButton:@"otherButton"
                informativeTextWithFormat:@"informativeTextWithFormat %@",@"text"
                ];
//ボタンを追加する
[alert addButtonWithTitle:@"add"];
//シートで表示
[alert beginSheetModalForWindow:[sender window]
            modalDelegate:self
            didEndSelector:@selector(endAlert)
            contextInfo:nil
            ];

}
//終わった後の処理
-(void)endAlert
{
NSLog(@"end");
}

@end

swift5.1

//NSAlert runModal/addButtonWithTitle
    //NSApplication.ModalResponse
    @IBAction func function009(sender: AnyObject) {
        //End even if there are other modals.
        NSApp.abortModal()
        
        //Create text
        let messageText:String = "Message text" as String
        let informativeText:String = NSLocalizedString("infoText", comment: "Information")
        
        //Create NSAlert
        let alert:NSAlert = NSAlert()
        alert.messageText = messageText
        alert.informativeText = informativeText
        alert.delegate = self
        alert.addButton(withTitle: "First") //ID 1000
        alert.addButton(withTitle: "Second")  //ID 1001
        alert.addButton(withTitle: "Third")  //ID 1002
        let response:NSApplication.ModalResponse = alert.runModal()
        
        //Modal start
        print("NSModalResponse %u",response)
        
        //Return the result if the modal is closed.
        //NSApplication.ModalResponse
        switch response {
        case .alertFirstButtonReturn: //Rightmost button
            print("First")
        case .alertSecondButtonReturn :    //Second button
            print("Second")
        case .alertThirdButtonReturn :       //Third button
            print("Third")
        default :
            break
        }
        
    }
//NSAlert runModal/addButtonWithTitle
    @IBAction func function009(sender: AnyObject) {
        //なにかモーダルがあっても終わり
        NSApp.abortModal()
        
        //テキストの作成
        let messageText:String = "Message text" as String
        let informativeText:String = "Information text" as String
        //NSAlertの作成
        let alert:NSAlert = NSAlert()
        alert.alertStyle = .WarningAlertStyle
        alert.messageText = messageText
        alert.informativeText = informativeText
        alert.delegate = self
        alert.addButtonWithTitle("First") //ID 1000
        alert.addButtonWithTitle("Second")  //ID 1001
        alert.addButtonWithTitle("Third")  //ID 1001
        let response:NSModalResponse = alert.runModal()
        //モーダル開始
        NSLog("NSModalResponse %u",response)
        //モーダルが閉じられた場合の結果が帰ってくる
        switch response {
        case 1000 :    //一番右のボタン
            NSLog("First")
        case 1001 :    //二番目
            NSLog("Second")
        case 1002 :       //三番目
            NSLog("Third")
        default :
            break
        }
    }