Hampton Lintorn Catlin

Macruby and Delegate Interfaces

No one can stop me from writing so many damn blogposts about macruby. I’m doing it because no one else out there seems to have any references on these things. So, if these articles have helped you out, just let me know in the comments.

In obj-c we define interfaces called “Delegates” that receive the actions of some other object. For instance, if you have an NSTableView (or UITableView), you can wire it up to a NSViewController that can optionally say “Hey, I can be a delegate for a table view”. It might look like this in obj-c.

@interface SearchViewController : UIViewController 

In this obj-c example, we have a class “SearchViewController” that extends “UIViewController”. Oh, by the way, “UI” prefixed things tend to be iOS interfaces and “NS” prefix means cocoa (os x). But, they are extremely similar and it doesn’t change the point here. Within the ”<” and ”>” we list the interfaces that we are declaring. Its basically saying “I’d be happy to receive actions as these types of delegates.” Using these delegates means you can pack a lot of interactivity into your application and when we are doing macruby development, we really need to have these guys on our team.

So! How do we make this work in macruby? Well, the weird answer (to me at least) is magically. Just define a macruby class!

class SearchViewController < NSViewController
          def tableView(table_view, setObjectValue:value, forTableColumn:column, row:row_index)
            doc = @documents[row_index]
            doc.name = value
          end
        end

And, wire up any delegations you want in the Interface Builder…. it just works.

I’m not sure what black magic, but to someone with obj-c experience it seems a bit creepy. So, read this blogpost and take heart… JUST WIRE IT UP.


Comments

Dec 4, 2010
bobobo1618 said...
I now feel fortunate that I do not have to use Obj-C. It seems bad.