macOS/iOS API解説

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

目次

runModal

INDEX>AppKit>NSApplication

指定したウインドウでモーダルイベントループを立ち上げます

Swift

open func runModal(for window: NSWindow) -> NSApplication.ModalResponse
-(NSInteger)runModalForWindow:(NSWindow *)aWindow

解説

指定したウインドウでモーダルイベントループを立ち上げます。

返り値

( NSInteger )

整数値

引数

( NSWindow * )aWindow

ウインドウ

フレームワーク

ApplicationKit

クラス

NSApplication

使用可能

10.0

編集時のバージョン

10.14.5
Swift4.2

参照

例文

Swift4.2

//リターンコードの設定
    fileprivate let RETURN_NO  :NSApplication.ModalResponse = NSApplication.ModalResponse(rawValue: 0) // モーダルの戻り判定 NO
    fileprivate let RETURN_YES :NSApplication.ModalResponse = NSApplication.ModalResponse(rawValue: 1) // モーダルの戻り判定 YES
    
  //マルチウインドウ環境で、ボタンを押した瞬間のメインスクリーンの座標位置がおかしくて、ウインドウを一旦移動してやらないとうまくいかない
    //NSApplication runModal
    var modalWindow : NSWindow!
    var windowFlag:Bool = false
    var windowController : NSWindowController!
    @IBAction func function008(_ sender: AnyObject) {
        let screenArray : [AnyObject] = NSScreen.screens
        for value in screenArray {
            print("screen size = (\(Float(value.frame.size.width)) ,\(Float(value.frame.size.height)))")
            NSLog("screen size=  (%.2f✕%.2f)",
                  Float(value.frame.size.width),
                  Float(value.frame.size.height) )
        }
        //
        
        sender.window.makeKeyAndOrderFront(nil)
        sender.window.becomeKey()
        //
        let theScreen : NSScreen = (NSScreen.main)!
        //if (theScreen){
        print(theScreen.frame)
        //}else {
        //      print("No screen")
        let myScreen : NSScreen = sender.window.screen!
        
        print(myScreen.frame)
        
        //}
        for value in screenArray {
            NSLog("screen size=  (%.2f✕%.2f)",
                  Float(value.frame.size.width),
                  Float(value.frame.size.height) )
        }
        //別のウインドウ
        if(!self.windowFlag){
            self.modalWindow = NSWindow(contentRect: NSMakeRect(0, 0, NSScreen.main!.frame.midX, NSScreen.main!.frame.midY), styleMask: [.closable,.titled,.resizable], backing: .buffered, defer: false,screen: sender.window.screen!)
            //windowArray.add(modalWindow as Any) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
            
            self.windowController = NSWindowController(window:modalWindow)
            self.modalWindow.title = "New Window"
            //self.modalWindow.isOpaque = false
            self.modalWindow.center()
            self.modalWindow.isMovableByWindowBackground = true
            //self.modalWindow.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.7)
            self.modalWindow.makeKeyAndOrderFront(nil)
            
            //Create a button
            let theButton : NSButton = NSButton(frame: NSMakeRect(100.0, 0.0, 100.0, 30.0))
            theButton.title = "Action"
            theButton.bezelStyle = NSButton.BezelStyle.rounded
            
            //theButton.action =  #selector(ViewController.viewAction001(_:))
            theButton.action =  #selector(ViewController.action008(_:))
            theButton.target = self
            
            //Add button to window
            modalWindow.contentView!.addSubview(theButton)
            
            self.windowController = NSWindowController(window:self.modalWindow)
            
            self.windowController.showWindow(self)
            
            self.windowFlag = true
        }else{//windowがすでに生成されている場合はNSWindowControllerを使用してwindowを表示
            self.windowController!.showWindow(nil)
        }
        
        print(modalWindow as Any)
        
        let anApplication = MyApplication.shared
        let result = anApplication.runModal(for: modalWindow)
        
        //モーダルが閉じられた場合の結果が帰ってくる
        
        switch result {
        case RETURN_NO :
            NSLog("NO")
            break
        case RETURN_YES :
            NSLog("YES")
        default :
            break
        }
        
    }
#import "Controller.h"

@implementation Controller
NSInteger modaiReturnCode;

- (IBAction)pushButton:(id)sender
{
	modaiReturnCode=[[NSApplication sharedApplication] runModalForWindow:panel];	
	NSLog(@"modaiReturnCode %d",modaiReturnCode);
}

- (IBAction)stopButton:(id)sender
{
	[[sender window] orderOut:self];
	[NSApp stopModalWithCode:modaiReturnCode];
}

@end
//Modal windowの表示
    //NSApplication runModalForWindow
    @IBAction func function008(sender: AnyObject) {
        //newWindowはNIBで作られているwindow
        //@IBOutlet weak var newWindow: NSWindow!
        let anApplication = MyApplication.sharedApplication()
        anApplication.runModalForWindow(newWindow)
    }
    //NSApplication stopModal
    @IBAction func function009(sender: AnyObject) {
        NSLog("%@",newWindow)
        let anApplication = MyApplication.sharedApplication()
        anApplication.stopModal()
        newWindow.orderOut(self)
    }