簡體   English   中英

帶有NSWindowController的菜單欄應用

[英]Menubar app with NSWindowController

我目前正在嘗試構建菜單欄應用。 因此,我需要一個NSWindowController作為登錄字段。 按下菜單項時,必須可以打開此NSWindowController;當用戶單擊“取消”時,也必須關閉該窗口。

我使用了showWindow(self)NSApp.hide(self)但這對我不起作用。 所以有人知道我可以嘗試什么嗎?

假設您正在使用情節提要

  • NSWindowController添加到情節NSWindowController中,並取消選中visible at launch窗口visible at launchvisible at launch的內容。
  • AppDelegate創建一個屬性windowController

     var windowController : NSWindowController! 
  • AppDelegate創建一個IBAction

  • 在行動中獲得主要的故事板

     let mainStoryBoard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) 
  • 然后實例化並分配窗口控制器(標識符必須與情節提要標識符匹配)

     windowController = mainStoryBoard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "Login")) as! NSWindowController 
  • 獲取關聯的視圖控制器( LoginController是視圖控制器的自定義類)

     let loginController = windowController.window!.contentViewController as! LoginController 
  • 顯示主視窗

     windowController.showWindow(self) 
  • 在Interface Builder中,將NSMenuItem連接到First Responder (紅色多維數據集),然后再連接到創建的IBAction

您可以使用紅色的close按鈕關閉窗口,或者需要添加自定義邏輯。

如果使用XIB,請創建一個NSWindowController子類,並使用windowController = MyWindowController(window: nil)加載XIB,然后使用NSApp.activate(ignoringOtherApps: true)激活您的應用, NSApp.activate(ignoringOtherApps: true)使用let controllerWindow = windowController.window! NSApp.activate(ignoringOtherApps: true)獲得關聯的窗口let controllerWindow = windowController.window! 並使用controllerWindow.makeKeyAndOrderFront(self)顯示窗口

抱歉,您的代碼在我的項目中崩潰了。 我做錯了什么?

截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM