swift - 应用程序关闭时重新启动 View

标签 swift xcode macos statusbar

我正在为 Mac 创建一个带有设置 View 的状态栏应用程序。

我已经创建了一个 NSMenuItem 来启动设置,但我没有找到任何解决方案来启动此 View 。

我尝试过的:

NSWorkspace.shared().launchApplication("AppName")

StatusMenuController.swift

func showSettings() {

    var mainWindow: MainWindowController!
    mainWindow = MainWindowController()
    mainWindow.showWindow(nil)
}

MainWindowController.swift

override func windowDidLoad() {
    super.windowDidLoad()

    self.window?.center()
    self.window?.makeKeyAndOrderFront(nil)
    NSApp.activate(ignoringOtherApps: true)
}

最佳答案

我看到很多情况下都会遇到这个问题,我找到了一个我不完全理解的解决方案,但我会尽力解释。

以下是 macOS 中文档的管理方式:

Document architecture in macOS

根据我的经验,在创建新的 Mac 应用程序时,您需要选中“创建基于文档的应用程序”复选框,以拥有 NSDocument 类(您也可以添加稍后)这将处理您的 View 如何显示或不显示。

将此 NSDocument 类添加到我的项目中可以使以下代码正常工作(以前不能):

let storyboard = NSStoryboard(name: "Main", bundle: nil)
var windowController: NSWindowController!
windowController = storyboard.instantiateController(withIdentifier: "ScanWindowController") as! NSWindowController
windowController.showWindow(nil)

关于swift - 应用程序关闭时重新启动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48628895/

相关文章:

Swift 计算属性返回底层数组的副本

ios - 无法为 '[OnboardingViewController]' 类型的值添加下标

xcode - 从另一个 View Controller 访问数组

javascript - 用于自动化显示对话框的 Mac OS X Javascript

ios - 以编程方式在 ViewController 中使用 NavigationController

ios - "Computed property must have an explicit type"错误 : how to list files in Swift 2. x?

ios - 如何在不使用多个 webview 的情况下更改 webview 的 url

ios - SDK iOS 8.0 中的产品类型单元测试包需要代码签名

mysql - 从命令行关闭 mysql [Windows][DOS]

macos - 隐藏/显示隐藏文件的 Mac shell 脚本