Can I build RubyMotion apps with Interface Builder?
-
05-06-2021 - |
Domanda
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.
Soluzione
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 :
- Create your
.xib
file using XCode (no need to create a project, just use File|New...|File) and save it into theresources
folder. As there is no support for outlets yet, be careful to set thetag
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). - RubyMotion will take care of compiling your
.xib
file into a.nib
file, so enjoy :) - In your
UIViewController
derived class, load the nib usingloadNibNamed:owner:options:
, as shown below. - In
viewDidLoad
, fetch your various components usingviewWithTag:
and add events handlers usingaddTarget:action:forControlEvents:
,as show below. 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
Altri suggerimenti
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/
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.
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.)
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
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