In this content, describe how to create a simple application. This content have some essences to create an application using MacRuby.
Show an application’s User Interface in following figure. We will call this application “StopWatch”.
The “StopWatch” has below behaviors.
- Run a timer if start button is clicked.
- Stop a timer if stop button is clicked.
- A timer value is displayed in Text Field.
To start the design, need some operations in Xcode.
Choose a MainMenu.xib.
Choose a [Window - StopWatch] to display Window for design.
Click an icon in toolbar like the following figure. After that, Object Library is displayed.
You have been completed to prepare to design. Then, place the User Interface parts from Object Library to Window.
Connect the Outlets
The “StopWatch” application sets a timer value into Text Field. However, if Text Field is just placed on Window, cannot set a value into there. We should use the Outlets to set a value or to get a status of User Interface parts.
To use the Outlets, need to write a program code. Choose a AppDelegate.rb. After that, add a
attr_accessor :textField to AppDelegate class.
1 2 3
Return to MainMenu.xib screen, connect the Outlet. Press control key and drag from App Delegate to Text Field.
A list of Outlets will be displayed, and then choose a
textField to connect it.
You can set/get the value of the Text Field through the textField accessor.
Connect the Actions
When you clicked start/stop button, still nothing happens. You need to set the behaviors when the buttons are clicked.
Choose an AppDelegate.rb. After that, add
stopTimer methods as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Return to MainMenu.xib screen, connect the Actions. Press control key and drag from start button to App Delegate.
A list of Actions will be displayed, and then choose a
startTimer to connect it.
Connect stop button to
stopTimer in the same way. Each methods will be called when the button is clicked.
startTimer are also known as the action method.
senderargument. If methods does not have a
senderargument, it is not recognized as an action method.
Use the timer
If you do something at a constant interval, use the NSTimer.
When use a timer as shown below, you will be able to handle at a constant interval.
1 2 3 4 5 6 7 8 9 10
Generate a timer at startTimer and stop a timer at stopTimer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
@timer.invalidate to stop a timer. Invoke
textField.setStringValue(string) to set string to Text Field.
“Stopwatch” application is complete!
Change to “StopWatch” from “Deployment” in Scheme. After that, Click [Run]. “Stopwatch” application will be running!!