macOS/iOS API解説

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

目次

endSheet:returnCode:

INDEX>AppKit> NSWindow

シートウインドウを指定して、モーダルセッションを終わります
- (void)endSheet:(NSWindow *)sheet
      returnCode:(NSInteger)returnCode
func endSheet(_ sheet: NSWindow,
   returnCode returnCode: Int)

解説

シートウインドウを指定して、文書モーダルセッションを終わります。リターンコード(NSInteger)をデリゲートオブジェクトに渡します。

下記のサンプルでは、「close」ボタンを押した時の動作です。
f:id:jjj777:20150318211828g:plain

返り値

なし

引数

( NSWindow * )sheet

シートウインドウ

( NSInteger )returnCode
returnCode returnCode: Int

リターンコード

フレームワーク

ApplicationKit

クラス

NSApplication

使用可能

10.0

参照



例文

Objective-C

#import "MyObject.h"
@implementation MyObject

- (IBAction)myAction:(id)sender
{
	[NSApp 	beginSheet:sheetWindow
	 modalForWindow	:[sender window]
	 modalDelegate	:self
	 didEndSelector	:@selector(sheetDidEnd:returnCode:contextInfo:)
	 contextInfo	:nil
	 ];
}
- (IBAction)closeSheet:(id)sender
{
	NSLog(@"closeSheet");
	//OKボタンはtagを1にしてある(NSOKButton=1)
	//Cancelボタンはtagを0にしてある(NSCancelButton=0)
	//両方のボタンをこのメソッドに接続しておいて...
	[NSApp endSheet:sheetWindow returnCode:[sender tag]];
	[sheetWindow close];
}

- (void)sheetDidEnd:(NSWindow*)sheet 
                returnCode:(NSInteger)returnCode 
                contextInfo:(void*)contextInfo
{
		//ここでどのボタンが押されたかを見分ける
		//そうするとボタン増やしてもメソッド増やさなくてOK
		switch (returnCode) {
			case NSOKButton:
				NSLog(@"OK Button Pressed",returnCode);
				break;
			case NSCancelButton:
				NSLog(@"Cancel Button Pressed",returnCode);
				break;
			default:
				break;
		}
        

}

@end
    //NSWindow beginSheet:completionHandler:
    //NSWindow attachedSheet
    //NSWindow endSheet:returnCode:
    func buttonAction042_2(sender: AnyObject){
        //シートの親ウインドウ
        let parentWindow : NSWindow = (sender as NSButton).window!.sheetParent!
        
        if (parentWindow.attachedSheet != nil){
            NSLog("attachedSheet %@", parentWindow.attachedSheet!)
            //->attachedSheet <NSWindow: 0x6080001e0900>
        }
        //シートの親ウインドウに対して、シートの終了
        parentWindow.endSheet((sender as NSButton).window!, returnCode: NSModalResponseStop )
//            NSModalResponseStop                 = (-1000),
//            NSModalResponseAbort                = (-1001),
//            NSModalResponseContinue             = (-1002),
        
        
    }
    func buttonAction042(sender: AnyObject){
        
        var theWindow : NSWindow = (sender as NSButton).window!
        var sheetWindow : NSWindow = NSWindow(
            contentRect: NSMakeRect(0.0, 0.0, 200, 150),
            styleMask: NSBorderlessWindowMask,
            backing: .Buffered,
            defer: false)
        sheetWindow.backgroundColor = NSColor.redColor()
        //ボタンを作成
        var sheetButton : NSButton = NSButton(frame: NSMakeRect(50.0, 2.0, 80.0, 30.0))
        sheetButton.title = "Close"
        sheetButton.bezelStyle = NSBezelStyle.RoundedBezelStyle
        sheetButton.action = Selector("buttonAction042_2:")
        sheetWindow.contentView.addSubview(sheetButton)
        
        theWindow.beginSheet(sheetWindow, completionHandler:{responseCode  in
                if (responseCode == NSModalResponseStop) {
                    NSLog("NSModalResponseStop")
                }else{
                    NSLog("Other")
                }
            })

    }
    @IBAction func function042(sender: AnyObject) {
        var aWindow : NSWindow = NSWindow(
            contentRect: NSMakeRect(0.0, 0.0, 300, 200),
            styleMask: NSTitledWindowMask |
                        NSClosableWindowMask |
                        NSMiniaturizableWindowMask |
                        NSResizableWindowMask,
            backing: .Buffered,
            defer: false)
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        
        //ボタンを作成
        var theButton : NSButton = NSButton(frame: NSMakeRect(100.0, 2.0, 100.0, 30.0))
        theButton.title = "Sheet"
        theButton.bezelStyle = NSBezelStyle.RoundedBezelStyle
        theButton.action = Selector("buttonAction042:")
        aWindow.contentView.addSubview(theButton)
        
        
        
        //ウインドウの表示
        aWindow.center()//ウインドウをスクリーンの中心に
        aWindow.title = "ウインドウタイトル"//タイトル設定
        aWindow.orderFront(self)//前面に
     
    }