几分钟内就可以在macOS应用程序中添加首选项窗口

网友投稿 582 2022-10-29

几分钟内就可以在macOS应用程序中添加首选项窗口

几分钟内就可以在macOS应用程序中添加首选项窗口

Preferences

Add a preferences window to your macOS app in minutes

Just pass in some view controllers and this package will take care of the rest.

Requirements

macOS 10.12+Xcode 9.3+Swift 4.1+

Install

SPM

.package(url: "https://github.com/sindresorhus/Preferences", from: "0.1.0")

Carthage

github "sindresorhus/Preferences"

CocoaPods

pod 'Preferences'

Usage

Run the PreferencesExample target in Xcode to try a live example.

First, create a couple of view controllers for the preference panes you want. The only difference from implementing a normal view controller is that you have to add the Preferenceable protocol and implement the toolbarItemTitle and toolbarItemIcon getters, as shown below.

GeneralPreferenceViewController.swift

import Cocoaimport Preferencesfinal class GeneralPreferenceViewController: NSViewController, Preferenceable { let toolbarItemTitle = "General" let toolbarItemIcon = NSImage(named: .preferencesGeneral)! override var nibName: NSNib.Name? { return NSNib.Name("GeneralPreferenceViewController") } override func viewDidLoad() { super.viewDidLoad() // Setup stuff here }}

AdvancedPreferenceViewController.swift

import Cocoaimport Preferencesfinal class AdvancedPreferenceViewController: NSViewController, Preferenceable { let toolbarItemTitle = "Advanced" let toolbarItemIcon = NSImage(named: .advanced)! override var nibName: NSNib.Name? { return NSNib.Name("AdvancedPreferenceViewController") } override func viewDidLoad() { super.viewDidLoad() // Setup stuff here }}

In the AppDelegate, initialize a new PreferencesWindowController and pass it the view controllers. Then add an action outlet for the Preferences… menu item to show the preferences window.

AppDelegate.swift

import Cocoaimport Preferences@NSApplicationMainfinal class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet private weak var window: NSWindow! let preferencesWindowController = PreferencesWindowController( viewControllers: [ GeneralPreferenceViewController(), AdvancedPreferenceViewController() ] ) func applicationDidFinishLaunching(_ notification: Notification) {} @IBAction func preferencesMenuItemActionHandler(_ sender: NSMenuItem) { preferencesWindowController.showWindow() }}

API

protocol Preferenceable: AnyObject { var toolbarItemTitle: String { get } var toolbarItemIcon: NSImage { get }}class PreferencesWindowController: NSWindowController { init(viewControllers: [Preferenceable]) func showWindow() func hideWindow()}

FAQ

How is it better than MASPreferences?

Written in Swift. (No bridging header!)Swifty API using a protocol.Fully documented.The window title is automatically localized by using the system string.Less code (and less chance of bugs) as it uses NSTabView instead of manually implementing the toolbar and view switching.

Related

Defaults - Swifty and modern UserDefaultsLaunchAtLogin - Add "Launch at Login" functionality to your macOS appDockProgress - Show progress in your app's Dock icon

You might also like my apps.

License

MIT © Sindre Sorhus

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Darwin中socket接收前使用select等待
下一篇:哎哟不行了,让我笑一下
相关文章

 发表评论

暂时没有评论,来抢沙发吧~