[英]Menubar app with NSWindowController
我目前正在嘗試構建菜單欄應用。 因此,我需要一個NSWindowController作為登錄字段。 按下菜單項時,必須可以打開此NSWindowController;當用戶單擊“取消”時,也必須關閉該窗口。
我使用了showWindow(self)
和NSApp.hide(self)
但這對我不起作用。 所以有人知道我可以嘗試什么嗎?
假設您正在使用情節提要
NSWindowController
添加到情節NSWindowController
中,並取消選中visible at launch
窗口visible at launch
時visible 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.