22

Ruby Motion just came out, and the screencast doesn't seem to say anything about whether or not you can use Interface Builder to build your interfaces and integrate them in your RubyMotion project. Is such a thing possible? Or do I really have code everything by hand, not to mention maintain to different pieces of code for iPhone/iPad?

My rep isn't high enough to create a rubymotion tag, so please help out if appropriate.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848

6 Answers6

38

I'd just like to point out that RubyMotion 1.3 now support automatic compilation of .xib files that you put into the resources folder. So now the workflow becomes :

  1. Create your .xib file using XCode (no need to create a project, just use File|New...|File) and save it into the resources folder. As there is no support for outlets yet, be careful to set the tag property for each control you want to use in your code (you'll find in the property sheet of each component under the "View" header).
  2. RubyMotion will take care of compiling your .xib file into a .nib file, so enjoy :)
  3. In your UIViewController derived class, load the nib using loadNibNamed:owner:options:, as shown below.
  4. In viewDidLoad, fetch your various components using viewWithTag: and add events handlers using addTarget:action:forControlEvents:,as show below.
  5. As a bonus, next time you want to edit your xib, just do open resources/MyView.xib, it will only launch the good parts of XCode.

    class CalculatorViewController < UIViewController
        def loadView
            views = NSBundle.mainBundle.loadNibNamed "Keyboard", owner:self, options:nil
            self.view = views[0]
        end
    
        def viewDidLoad
            button = view.viewWithTag 1
            button.addTarget self, action:'buttonTapped:', forControlEvents:UIControlEventTouchUpInside
        end
    
        def buttonTapped(button)
            # ...
        end
    end
    
Nicolas Lehuen
  • 808
  • 1
  • 7
  • 9
6

Yes you can use Interface Builder in RubyMotion.

.xib files that are located in the resources directory are automatically compiled into .nib files and available to your project.

There is even a way to support outlets if you are so inclined :

https://github.com/yury/ib#readme
http://ianp.org/2012/05/07/rubymotion-and-interface-builder/

Stephan
  • 41,764
  • 65
  • 238
  • 329
dmin7b5
  • 1,031
  • 1
  • 9
  • 5
1

But if you really want to use IB then you could still probably use it to lay out your UI and just call

NSBundle.mainBundle.loadNibNamed(MY_NIB_NAME, owner:self, options:nil)

or similar. I don't think that RubyMotion does any name mangling so you can use your ruby classes in IB by explicitly setting them.

Ian Phillips
  • 163
  • 1
  • 6
  • maybe you could say a little more about how you'd compile an XIB into an NIB for use in a RubyMotion project? that seems to be the tricky part... – Alexander Wallace Matchneer May 04 '12 at 17:03
  • You can just use `ibtool`. The tricky part is connecting up the outlets and actions as you can't use the drag-and-drop features of XCode. – Ian Phillips May 05 '12 at 21:12
  • I tried doing that, passing the options `--compile` and `--flatten NO`, but it keeps throwing an `NSInternalInconsistencyException` at me. I was loading it with `myViewController.alloc.initWithNibName("MyViewController", bundle:nil)` How exactly do you run `ibtool` to get a loadable nib file? – Victor Jalencas May 06 '12 at 12:24
1

You can probably build the interface in IB and call the command-line tools to compile the XIB to a NIB.

However, you aren't going to be able to connect outlets or assign actions because IB can't parse the Ruby source files for the IBOutlet and IBAction tokens. Normally, the NIB loading code makes those connections for you after loading your NIB. You'll have to do that yourself, in code.

Of the two glaring weaknesses in RubyMotion, to me this is the worst. (The other is that lack of code completion will make writing to the Cocoa APIs really tedious.)

Steve Madsen
  • 13,465
  • 4
  • 49
  • 67
  • The next version of the ruby mine IDE supports ruby motion development and auto completion of Cocoa APIs. So this might help you – Rubinsh Nov 18 '12 at 22:44
1

Cappuccino had the same problem. They developed a tool called XcodeCapp: https://github.com/cappuccino/cappuccino/tree/master/Tools/XcodeCapp

It creates "dummy" Obj-C files that you can connect your outlets and actions to in IB, automatically parses them in the background and enables you to use IB to layout your Cappuccino UIs.

It should be possible to take a similar approach with RubyMotion (if you really want to use IB).

  • Johannes
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
0

There is someone who has made that:

Here is a tutorial video: https://www.youtube.com/watch?v=cOapNvehbg4

And this is the website https://github.com/yury/ib

Extra-Tipp: To find wrappers for rubymotion check this source

http://rubymotion-wrappers.com/

Hope this helps

Jan
  • 12,992
  • 9
  • 53
  • 89