April 22, 2011

Preferences Windows in Cocoa

The most of applications in Mac OS X have the Preferences window. Unfortunately, there is no component like NSPreferencePane in the AppKit. Instead, we want to use NSToolbar with custom delegate and selectable items.

Cocoa community has made some great implementations of Preferences window. Probably, the most popular are SS_PrefsController by Matt Legend Gemmell and Selectable Toolbar by Brandon Walkin. It's interesting that Matt's solution is extremely powerful and more advanced: i.e. you can create and build preference panes as separate Xcode projects. While Brandon's component is all-in-one and easy to use: you do not need to read any documentation to get started with it.

I had used these components for development until I went into iPhone development. In CocoaTouch you must use UIViewController class everywhere, and soon or later it becomes your best friend. After returning back to Cocoa on Desktop I realized that I really miss “standard” view controllers, so I started looking for new Preferences implementation. The main criteria was using NSViewController class for preference panes.

Google did not offer anything interesting, but I found great research of Preferences windows by James Huddleston. After reading that brilliant article, I decided to develop my own component meeting two requirements. First, it should be based on modern NSWindowController and NSViewController classes. Second, it should load preference panes lazily.

After some iterations, I developed components that I use for Preferences windows in CodeBox and Hunting. Here I would like to share what I have. Maybe this code will be helpful to somebody. Please find the Demo project at GitHub:

I'm sorry for the lack of any documentation at the moment. Please consider this code as work in-progress.

7 comments:

  1. Thank you very much. Works nicely, and I'm so glad to get away from BWToolKit (because of the Xcode 4 issues).

    ReplyDelete
  2. Nice work! I have used your code in one of my projects and it works very well.

    ReplyDelete
  3. FYI, the "great research of Preferences windows" link just leads to a landing page now. :-(

    ReplyDelete
    Replies
    1. Thanks for the heads up! I'm sorry for inconvenience. Please use Google cache.

      Delete
  4. Awesome piece of work! Fills in the gap left by Xcode 4 not supporting IB plugins (and rendering BWToolkit unusable). This feels a lot like UITabBar in iOS - plain, simple and straight to the point.

    Thanks a lot for sharing this code, kind sir.

    ReplyDelete
  5. Thank you very much, MASPreferences are great!

    ReplyDelete