In this article I will explain how to create a simple iOS application using RubyMotion. I will call this application “StopWatch”, and MacRuby version of this application is explained in “Creating a simple application”.
The “StopWatch” application has the following features.
This application has the user interface which are start buttion, stop button and label.
Run the timer if the start button is clicked.
Stop the timer if the stop button is clicked.
The timer value is displayed in the label.
Creating a New Application
The command-line interface is created as /usr/bin/motion if you install RubyMotion.
Runs as following command in Terminal, you may create the application template.
Implements the code which contains actions or user interface for iOS into this file.
UIKit Framework Reference may help you to implement user interface.
1. Create the UIWindow object
First step, creates the UIWindow object. The UIWindow class defines objects that manage and coordinate the windows an application displays on the screen.
1234567891011
classAppDelegatedefapplication(application,didFinishLaunchingWithOptions:launchOptions)@window=UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)# Sets the StopWatchViewController as rootViewController@window.rootViewController=StopWatchViewController.alloc.init@window.makeKeyAndVisibletrueendend
Sets the StopWatchViewController which inherited the UIViewController as rootViewController.
The UIViewController provides basic functions for view-management.
2. Implement the StopWatchViewController
Prepares the StopWatchViewController as following.
viewDidLoad method is called after the controller’s view is loaded into memory. Implements the user interface into viewDidLoad method. The UIView is used when lays out the user interface. You may retrive the UIView as following.
123456789
# retrive the UIViewview=self.view# configure background colorview.backgroundColor=UIColor.whiteColor# retrive the screen sizeframe=view.boundsheight=frame.size.heightwidth=frame.size.width
Lays out the user interface
Creates the user interface object after retriving the UIView, and you may lay out the user interface that invokes addSubview.
Let’s lay out the start/stop button and label.
Lays out the start button
1234567891011
# start button@start=UIButton.buttonWithType(UIButtonTypeRoundedRect)@start.frame=CGRectMake(0,300,width/2-20,30)@start.setTitle("start",forState:UIControlStateNormal)# connect an action method for start button@start.addTarget(self,action:"startTimer:",forControlEvents:UIControlEventTouchUpInside)view.addSubview(@start)
Lays out the stop button
1234567891011
# stop button@stop=UIButton.buttonWithType(UIButtonTypeRoundedRect)@stop.frame=CGRectMake(width/2+20,300,width/2-20,30)@stop.setTitle("stop",forState:UIControlStateNormal)# connect an action method for stop button@stop.addTarget(self,action:"stopTimer:",forControlEvents:UIControlEventTouchUpInside)view.addSubview(@stop)
Lays out the label
123456
# label for displaying timer value@label=UILabel.alloc.init@label.frame=CGRectMake(20,200,width-20,30)@label.font=UIFont.fontWithName("AppleGothic",size:24)@label.textAlignment=UITextAlignmentCenterview.addSubview(@label)
Implements the action methods
We can define the startTimer and stopTimer methods as following.
The above code creates and schedules a timer in startTimer that will execute the timerHandler method repetitively each 0.1 seconds. The timerHandler displays value into label, and stopTimer stops the timer by @timer.invalidate.
** IMPORTANT **:
Currently, if you use the data for a long time, you must keep as instance variables.
Refer: Runtime
Runs the StopWatch application
You may run the StopWatch application by rake command in Terminal.
classStopWatchViewController<UIViewControllerdefviewDidLoad# retrive the UIViewview=self.view# configure background colorview.backgroundColor=UIColor.whiteColor# retrive the screen sizeframe=view.boundsheight=frame.size.heightwidth=frame.size.width#--------------------# start button@start=UIButton.buttonWithType(UIButtonTypeRoundedRect)@start.frame=CGRectMake(0,300,width/2-20,30)@start.setTitle("start",forState:UIControlStateNormal)# connect an action method for start button@start.addTarget(self,action:"startTimer:",forControlEvents:UIControlEventTouchUpInside)view.addSubview(@start)#--------------------# stop button@stop=UIButton.buttonWithType(UIButtonTypeRoundedRect)@stop.frame=CGRectMake(width/2+20,300,width/2-20,30)@stop.setTitle("stop",forState:UIControlStateNormal)# connect an action method for stop button@stop.addTarget(self,action:"stopTimer:",forControlEvents:UIControlEventTouchUpInside)view.addSubview(@stop)#--------------------------# label for displaying timer value@label=UILabel.alloc.init@label.frame=CGRectMake(20,200,width-20,30)@label.font=UIFont.fontWithName("AppleGothic",size:24)@label.textAlignment=UITextAlignmentCenterview.addSubview(@label)enddefstartTimer(sender)if@timer==nil@time=0.0@timer=NSTimer.scheduledTimerWithTimeInterval(0.1,target:self,selector:"timerHandler:",userInfo:nil,repeats:true)endenddefstopTimer(sender)if@timer@timer.invalidate@timer=nilendenddeftimerHandler(userInfo)@time+=0.1string=sprintf("%.1f",@time)@label.text=stringendendclassAppDelegatedefapplication(application,didFinishLaunchingWithOptions:launchOptions)@window=UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)# Sets the StopWatchViewController as rootViewController@window.rootViewController=StopWatchViewController.alloc.init@window.makeKeyAndVisibletrueendend