启动时打开菜单栏应用程序(Mac OS 应用程序)[Swift,启动时打开 ViewController]

时间:2021-06-04 18:22:22

标签: swift macos button storyboard

我需要能够在启动时打开菜单栏应用程序。我目前正在使用 button.performClick(nil) 并且它有点工作,但它在错误的屏幕上/离开屏幕的方式启动 viewController。

那么在启动时弹出 ViewController 的最佳方法是什么。

所以基本上我希望这个窗口在启动时打开 LINK

1 个答案:

答案 0 :(得分:0)

所以我为我的问题找到了一个简单而肮脏的解决方法。

我有这个代码:(之前:https://i.imgur.com/ZIoMEBd.png

if let button = statusItem.button {
     button.performClick(nil)
}

但后来我添加了一个 0.5 秒的计时器,它解决了我的所有问题(您可能会使用 0.1 秒的计时器):(在 https://i.imgur.com/NBok96I.png 之后)

if let button = statusItem.button {
          _ = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (timer) in
               button.performClick(nil)
     }
}

注意:图片仅供视觉辅助