macOS/iOS API解説

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

目次

NSWorkspaceWillPowerOffNotification

INDEX>AppKit>NSWorkspace

パワーオフしようとするときの通知

解説

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.0

更新時のバージョン

OS X 10.10.3
Swift1.2

参照

例文

Objective-C

Swift

    //NSWorkspace NSWorkspaceDidWakeNotification
    //NSWorkspace NSWorkspaceWillSleepNotification
    //NSWorkspace NSWorkspaceWillPowerOffNotification
    let theLabel055 : NSTextField = NSTextField(frame: NSMakeRect(10.0, 100.0, 280.0, 30.0))
    
    var switch055 : Bool = false
    func notify055(notify:NSNotification) {
        NSLog("!! %@",notify.description)
        
        if notify.name == NSWorkspaceDidWakeNotification {
            theLabel055.stringValue = theLabel055.stringValue + "\n目覚めた"
            
        }
        if notify.name == NSWorkspaceWillSleepNotification {
            theLabel055.stringValue = theLabel055.stringValue + "\nスリープしようとした"
        }
        //電源オフしようとした
        if notify.name == NSWorkspaceWillPowerOffNotification {
            theLabel055.stringValue = theLabel055.stringValue + "\n電源オフしようとした"
        }
    }
    @IBAction func function055(sender: AnyObject) {
        var aWindow : NSWindow = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300, 200), styleMask: NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask, backing: .Buffered, defer: false , screen: NSScreen.mainScreen())
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄

        theLabel055.stringValue = "あああ"
        theLabel055.bordered = false
        theLabel055.canBecomeKeyView
        
        aWindow.contentView.addSubview(theLabel055)
        
        aWindow.contentView.addSubview(theView)
        aWindow.center()//ウインドウをスクリーンの中心に
        aWindow.orderFront(self)//前面に
        aWindow.makeKeyAndOrderFront(self)//表示
        
        let notificationName1 : String = NSWorkspaceDidWakeNotification
        let notificationName2 : String = NSWorkspaceWillSleepNotification
        let notificationName3 : String = NSWorkspaceActiveSpaceDidChangeNotification
        let notificationSelector : Selector = Selector("notify055:")
        
        if switch055 {
            //通知の監視をやめる
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName2, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName3, object: nil )
            NSLog("--------observe off")
            switch055 = false
            
        }else{
            //通知の監視を始める
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, forKeyPath: notificationName1, options:NSKeyValueObservingOptions.New, context: nil)
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName2, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName3, object: nil )
            NSLog("--------observe on")
            switch055 = true
        }
    }