Hampton Lintorn Catlin

Macruby IB Received Actions and IBAction

Here is another bit of undocumented macruby. If you are building a custom object in macruby (either extending objc object or a ruby object) and you want to list “Receive Action” in Interface Builder, then there is a little trick to make this happen. First, I had this:

def set_font_size(value)
          # blah
        end

But… I couldn’t hook it up in IB! It just wouldn’t list. This isn’t at all what I wanted! In obj-c, IB knows that its an action if it is defined like this:

-(IBAction)setFontSize:(int)i;

IBAction is really an alias of void, but its just a little trick that obj-c uses to figure out that we want to make it publicly available to IB. However, in macruby, we can’t specify the return type. After reading enough examples, I gave something crazy a try. What if the name of the arg matters?

def set_font_size(sender)
          # blah
        end

And there it appears. Damnit! Always call the arg “sender” and you will have your macruby methods show up as actions in interface builder. Macruby is too smart for its own good and the documentation is just…. lame. We need merbist to finish his book fast!