簡體   English   中英

工具欄、觸控欄和菜單無法識別從 AppDelegate 啟動 window

[英]Launching a window from AppDelegate is not recognised by toolbar, Touch Bar and menus

我正在嘗試以編程方式從 AppDelegate 啟動 window,特別是從服務提供商,它工作但不是很好,因為工具欄不起作用,觸摸欄不顯示並且我添加的自定義菜單項被禁用。 Apple 提供的默認菜單和菜單項有效。

我所有的觀點都是在故事板上創建的。 默認主 window 不是從 AppDelegate 啟動的。

我像這樣啟動 window。

guard let windowController = NSStoryboard.main?.instantiateController(withIdentifier: .windowController) as? WindowController else { return }

windowController.window?.title = data.title
windowController.viewController?.property = data
windowController.showWindow(self)

但是,如果我通過 segue 啟動,它會完美運行。

guard let splitViewController = NSApp.mainWindow?.contentViewController as? NSSplitViewController,
      let viewController = splitViewController.splitViewItems[1].viewController as? ViewController else { return }

viewController.anotherViewController.performSegue(withIdentifier: .segue, sender: data)

那么,我為什么不這樣做呢? 因為它從主 window 工作,然后程序啟動 window 阻止該代碼再次工作。 僅當只有一個 window 時才有效。

我希望它每次都能正常工作。 主 window 是鍵 window 還是后續的其他非主 windows 都沒有關系。

How can I make sure that I can launch a window and it is also recognised by the toolbar, Touch Bar and the custom menu items and it doesn't matter if there is only the main window or other windows which are not the main window but不同類型的 windows。

我誤解了mainWindow屬性,當它真正指向焦點 window 時,認為它指的是實際的主 window。 所以,我真正想要的主要 window 是windows數組中的第一個 window。

guard let mainWindowController = NSApp.windows.first?.windowController as? MainWindowController,
      let splitViewController = mainWindowController.contentViewController as? SplitViewController,
      let viewController = splitViewController.splitViewItems[1].viewController as? ViewController else { return }

viewController.anotherViewController.performSegue(withIdentifier: .segueIdentifier, sender: data)

菜單項、工具欄和觸控欄正在工作並響應用戶交互。

暫無
暫無

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

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