2017-07-03 6 views
0

Ich möchte verhindern, dass Benutzer das Hauptfenster schließen (und die App beenden), während ein Countdown läuft. Ich habe Beiträge darüber gelesen, aber leider funktioniert keine der beschriebenen Methoden. Ich denke, ich mache das Richtige, aber windowShouldClose wird niemals im Gegensatz zu den anderen beiden Funktionen aufgerufen. Ich bin verzweifelt. :) Hier ist mein Code in der NSWindowController:windowShouldClose (_ :) rief nie

import Cocoa 

class WindowController: NSWindowController, NSWindowDelegate { 

override func windowDidLoad() { 
    super.windowDidLoad() 
    print("Window did load") 
    self.window?.delegate = self 
} 

func windowShouldClose(sender: NSWindow) -> Bool { 
    print("Window should close") 
    let alert = NSAlert.init() 
    alert.addButton(withTitle: "No") 
    alert.addButton(withTitle: "Yes") 
    alert.informativeText = "Close the window?" 
    let response = alert.runModal() 
    if response == NSAlertFirstButtonReturn { 
     return true 
    } else { 
     return false 
    } 
} 

func windowWillClose(_ notification: Notification) { 
    print("Window will close") 
} 

func windowDidChangeScreen(_ notification: Notification) { 
    print("Window did change screen") 
} 

} 

Antwort

0

windowShouldClose(_:) heißt nie, weil man es erklärt, wie windowShouldClose(:)

In Swift 3 die Unterschrift des delegate method ist

func windowShouldClose(_ sender: NSWindow) -> Bool 
+0

Thanks a Menge! Vous êtes mon sauveur! :) – PhiGoLu