【问题标题】:How to programatically open Settings window in a macOS SwiftUI App如何以编程方式在 macOS SwiftUI 应用程序中打开设置窗口
【发布时间】:2021-03-29 00:40:57
【问题描述】:

我创建了一个简单的 macOS-only SwiftUI 应用程序,并根据Apple's instructions 添加了一个设置屏幕:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            RootView()
        }
        Settings {
            SettingsView()
        }
    }
}

有效:Preferences 选项显示在应用菜单中。现在,我还想以编程方式打开此设置窗口,例如单击按钮时。有没有办法做到这一点?

我希望NSApplication 中有一个方法,类似于 About 窗口,但似乎没有。

【问题讨论】:

    标签: macos swiftui window settings preferences


    【解决方案1】:

    通过查看应用菜单,我找到了这个解决方案:

    NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
    

    不过,我认为它可能无法通过使用私有 API 的 App Review,因此仍然欢迎使用不那么阴暗的替代方案。

    更新:这实际上已经通过了应用审核,所以我现在将其标记为答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 2021-01-05
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2013-01-13
      相关资源
      最近更新 更多