2015-08-31 113 views
0

我想通過單擊主窗口中的按鈕來快速顯示/隱藏窗口。起始窗口顯示窗口,但終端窗口不關閉窗口。我的appdelegate代碼給出:通過在Swift中單擊按鈕顯示/隱藏窗口

import Cocoa 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate { 

    @IBOutlet weak var window: NSWindow! 


    func applicationDidFinishLaunching(aNotification: NSNotification) { 
     // Insert code here to initialize your application 
    } 

    func applicationWillTerminate(aNotification: NSNotification) { 
     // Insert code here to tear down your application 
    } 

    var settingsController: SettingsController? 

    @IBAction func inSettings(sender: NSObject?) 
    { 
     settingsController = SettingsController(windowNibName: "SettingsController") 
     window.beginSheet(settingsController!.window!, completionHandler: nil) 
    } 
    @IBAction func outSettings(sender: NSObject?) 
    { 
     window.endSheet(settingsController!.window!) 
    } 

} 

SettingsController:

import Cocoa 

class SettingsController: NSWindowController { 

    override func windowDidLoad() { 
     super.windowDidLoad() 

     // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. 
    } 
} 

回答

0

使用endSheet結束一個文檔模式板材會議。就像這樣:

@IBAction func outSettings(sender: NSObject?) 
{ 
    settingsController!.window!.endSheet(settingsController!.window!) 
} 

編輯:您需要在您完成處理程序,以真正關閉這個窗口,你叫orderOut,像這樣:

@IBAction func inSettings(sender: NSObject?) 
{ 
    settingsController = SettingsController(windowNibName: "SettingsController") 
    window.beginSheet(settingsController!.window!) { 
     settingsController!.window!.orderOut(nil) 
    } 
} 
+0

襯頁是不是爲我工作 – Devan

+0

@Devan「不工作」是無益的。有沒有錯誤,如果是的話,哪一個?它什麼都不做?請詳細說明。 – Moritz

+0

澄清了'orderOut'的使用... – Aderstedt

0

斯威夫特3解決方法:

讓我們說你有WindowA和WindowB。你想打開WindowB,但首先你想隱藏WindowA。 用segue連接窗口。 (選擇「Show」作爲segues「Kind」屬性)而你需要一個靜態類來保持隱藏窗口。在WindowA覆蓋shouldPerformSegue並將WindowA保留爲靜態的NSWindow對象。

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { 

     YourStaticClass.WindowA = self.view.window 
     self.view.window?.orderOut(self) 

     return true 
    } 

orderOut(self)隱藏窗口。然後WindowB將被打開。

在窗口B的視圖控制器使用的功能來關閉窗口B,並顯示隱藏的窗口A:

@IBAction func btnBack_Click(_ sender: NSButton) { 

    YourStaticClass.WindowA?.makeKeyAndOrderFront(YourStaticClass.WindowA)  
    self.view.window?.close() 
} 
相關問題