Victus Spiritus

home

Building an App for Mac OS X Part 2

01 Mar 2011

This is a continuation of my exploration of developing a Mac OS X application, particularly a MacRuby app which has been designed well with deep integration into Cocoa objects implemented in objective C. The series is now two posts strong :)

The Cocoa framework is designed around a MVC architecture to aid in maintenance and reuse of applications. While I can respect the design decision, I'm confident I can get away with the VC framework for my first MacApp by embedding model data and methods within a controller. One of the major hurdles I had during my first sitting was figuring out how to bind GUI events to source code actions.

No matter what I tried I couldn't get the Controller object to show up on the list of available choices for any NSObjects I added to the graphical view builder (xib). While flying yesterday in my sardine can seat I managed to glean some insight from a few tutorials about developing for the mac (1,2, feed of all my favorite mac docs).

The first problem was solved by declaring the controller as a subclass of NSWindowController. I'm sure there are a variety of controller types I can inherit from depending on the project. But for now I'm going to follow the primordial law of nature KISS (keep it simple stupid), often conflated with Occam's Razor, after all it got me this far.

After that step the Controller class showed up in the in xib drop down interface for NSObjects. I was able to control click from the button view element to the Controller and setup a selector which sends an action (event) to the Controller from the view element (button clicked). I did the same for a text field which I initialized with the basic KeyBindings that provided the motivation for this app. Here's what the xib file interface looks like. The top left window is the .xib wiring and element list. The bottom left is the graphical view builder. The right two frames are the inspector (shows connections) and the Library (for adding objects/classes).

For the next post in the series I'd like to hunt down a satisfactory graphical representation of a Mac and Standard keyboard. My goal is to hook up all the key bindings to button presses, and persist those changes to disk. If something goes wrong there'll be a default button which will rename the .dict file to a backup allowing clients to safely return to default settings.

There's a good chance I'll port the app over to HotCocoa if for no other reason than to understand the DSL. I'll do so after I get to a point where I'm satisfied with cursory knowledge of Cocoa's API. Depending on HotCocoa's maturity and ability to deploy to the Mac AppStore, I'll decide later whether it's adequate to satiate my app building interests.