应用关闭时重新启动视图

时间:2018-02-05 18:14:20

标签: swift xcode macos statusbar

我正在使用设置视图为Mac创建状态栏应用。

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

我尝试过:

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)
}

1 个答案:

答案 0 :(得分:0)

我发现在很多情况下都遇到了这个问题,我找到了一个我不完全理解但我会尝试解释的解决方案。

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

Document architecture in macOS

根据我的经验,您需要在创建新的Mac应用程序时选中"创建基于文档的应用程序" 框,以获得NSDocument课程(您可以也可以稍后添加),它将处理您的视图的显示方式。

将这个NSDocument类添加到我的项目中使得以下代码工作(并且之前没有):

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