Sunday, March 15, 2009

iPhone Countdown App

Open up Xcode and click: File->New Project.


Select: iPhone OS ->Applications-> View-Based Application and name it whatever you would like.


Lets start with the UIView.


Double Click <YourProject>.xib” to launch “Interface Builder”



  • Click: Tools -> Reveal In Document Window -> View
  • Click: Tools -> Attributes Inspector
  • Select the background attribute and set it to Black


Now add a UILabel, it should be as simple as dragging it out of from Tools->Library.


To reference this label in our code we need to change its outlet id.



  • In the document window “File’s Owner”
  • Click: Tools->Idenity Inspector
  • In the inspector click the + under “Class Outlets”
  • Change myOutlet1 to “countdownLabel”
  • Change id to UILabel
  • Click enter to make sure they commit


Now lets generate the code for the controller we just created.



  • Still in interface builder make sure “File’s Owner” is still selected in the Document Explorer
  • Click: File->Write Class Files
  • Make sure you select your active target, which should be the name of your program.


Now we need to wire up the Label in Interface Builder to the UILabel in the class file



  • Reopen Interface Builder
  • Select the Label in the View or in Document Window
  • Click: Tools -> Connection Inspector
  • Move your mouse cursor over the empty circle to the right of text “New Referencing Outlet” (the circle will change to a Plus(+))
  • Mouse Click then drag the Plus (+) to “File’s Owner” in Document Window
  • When you release select countdownLabel in the popup list
  • Click: File -> Save then close Interface Builder


Creating the timer



  • In xCode open
  • In <yourProject>Controller.h add the line -(void)updateLabel; right before the line @end
  • In <yourProject>Controller.c add the lines


-(void)updateLabel {

}

right before the line @end



  • In <yourProject>Delegate.h add the field “NSTimer *timer;” and the method signature “-(void) onTimer;”
  • In <yourProject>Delegate.m



  • Start the timer in “applicationDidFinishLaunching”
  • Add the method onTimer that will update the label in the view controller
  • Invalidate the timer in the method “applicationWillTerminate”
  • release the timer in dealloc


Updating the label



  • Open <yourProject>Controller.c
  • Add a font in method “viewDidLoad”
  • Add code to get current time and update label in our new “updateLabel” method.


Build & Go!

0 comments:

Post a Comment