HTTPS clone URL
Subversion checkout URL
Creating a simple application
- Boxed Class
- Bug Triage
- Common issues
- Compiling MacRuby Code
- Contributing to MacRuby
- Creating a simple application
- Differences between MacRuby and RubyCocoa
- Dispatch Module
- Dispatch::Group Class
- Dispatch::Queue Class
- Dispatch::Semaphore Class
- Dispatch::Source Class
- Embedding MacRuby in your application
- How Does MacRuby Work?
- Loading Objective C Frameworks and Bundles
- Mac App Store Submission
- macirb tips and tricks
- MacRuby Projects
- MacRuby Resources
- MacRuby Tutorial
- Moving an Xcode Project to MacRuby from Objective C
- Original Constants
- Original Methods
- Pages from the Trac Wiki
- Pointer Class
- Ruby compatibility
- Sandbox Class
- Setting up MacRuby
- Snippets For Beginners
- Special Behaviours
- Starting ruby interactive
- Testing your Objective C library
- Troubleshooting MacRuby
- Using DTrace
- Why MacRuby?
- Working with NSOperationQueue
Clone this wiki locally
In this article we will explain how to create a simple application using MacRuby.
Here is the user interface of the application we will build. We will call this application "StopWatch".
The "StopWatch" application has the following features.
- 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 text field.
We will start by designing the user interface.
In the Xcode project, choose MainMenu.xib.
Select [Window - StopWatch] to display the window object.
Click on the following View toolbar item in order to display the object library.
Now we are reading to start designing the user interface. We can simply drag the controls from the object library to the window.
The "StopWatch" application sets the timer value in the text field. However, placing the text field on the window is not enough. We need to connect the text field with the actual application code. UI controls connected to code are called outlets.
In order to create an outlet, we need to write code. Select the AppDelegate.rb file, then add the
attr_accessor :textField line inside the AppDelegate class.
class AppDelegate attr_accessor :window attr_accessor :textField # This creates an outlet named textField
Now we can go back to the MainMenu.xib file and connect the outlet. Maintain the control key and drag the App Delegate object to the text field.
A list of outlets will be displayed. You can select
Now, we can retrieve and update the value of the text field in our code by using the respective
textField accessor methods.
At this point, if you click on the start or stop buttons, nothing happens. It's perfectly normal as we haven't written the code for these features.
Select the AppDelegate.rb file. We can define the
stopTimer methods as following.
class AppDelegate attr_accessor :window attr_accessor :textField # Outlet def applicationDidFinishLaunching(a_notification) # Insert code here to initialize your application end def startTimer(sender) # This method is called when the start button is clicked. end def stopTimer(sender) # This method is called when the stop button is clicked. end end
These methods are called actions. Actions are special methods defined in code that are connected to UI controls, and called when something happens with the control.
Let's go back to the MainMenu.xib file to connect the actions. Like we connected the outlet, we can maintain the control key and drag the start button to the App Delegate object.
A list of actions will be displayed. Here, you select
You can connect the stop button using the same way. After that, the methods will be called when the buttons will be clicked by the user.
startTimer are also known as the action method.
Important: when you are defining an action method, you must provide a
sender argument. If the method does not have a
sender argument, it won't be recognized as an action by Xcode.
The NSTimer class can be used in order to perform a certain action at a constant interval.
The following code creates and schedules a timer that will execute the
timerHandler method repetitively each 0.1 seconds.
@timer = NSTimer .scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerHandler:", userInfo: nil, repeats: true) def timerHandler(userInfo) # Handler end
Now, we can create the timer in
startTimer, and stop the timer in
class AppDelegate attr_accessor :window attr_accessor :textField # Outlet def applicationDidFinishLaunching(a_notification) # Insert code here to initialize your application end def startTimer(sender) if @timer.nil? @time = 0.0 @timer = NSTimer .scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerHandler:", userInfo: nil, repeats: true) end end def stopTimer(sender) if @timer @timer.invalidate @timer = nil end end def timerHandler(userInfo) @time += 0.1 string = sprintf("%.1f", @time) textField.setStringValue(string) end end
@timer.invalidate method is used to stop the timer. The
textField.setStringValue(string) method is used to change the text field to a new string value.
Congratulations, our "Stopwatch" application is now complete!
We can change the scheme to "Deployment". After that, Click [Run]. "Stopwatch" application will be running!