Adventures in code

First Ruby Motion App

I recently dived into rubymotion head first and since there is a definite lack of quality tutorials out there I decided to outline my first ruby motion project here in the hopes it will help someone. The goal of the project was simple, display hello world to the user. My first challenge was setting up the app delicate. The following code is the very generic setup the is in almost every ruby motion application.

1
2
3
4
5
6
def application(application,didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = HomeController.new
    @window.makeKeyAndVisible
    true
  end

This will initialize the window and set the initial controller to the homeController. Then in order for this to work I needed to define the HomeController, so in the controllers/home_controller.rb I define my controller.

1
2
3
4
5
6
7
class HomeController < UIViewController
  def loadView
    self.view = UIView.alloc.init
  end
  def viewDidLoad
  end
end

This defines the homecontroller and initializes the view local variable. This variable will be used to add UI elements to the view. So next is adding a UILabel to this view using the following code

1
2
3
label = UILabel.alloc.initWithFrame([[15,100],[140,70]])
label.text = "helloworld"
view.addSubview(label)

This code initializes a new UILabel in the given frame, adds the text, and finally adds it to the view initialized earlier. And thats it! We now have our generic helloworld application next thing to do: put it on the app store and make 1 million dollars.