Friday, April 10, 2009

Hello World! Your Second iPhone Application!







So you think Tutorial #1 was easy? Well here comes #2.

First open up Xcode again and make a View Based application. Name it HelloWorld.

Look under your classes folder and click on HelloWorldViewController.h.


When making applications you'll have a .h and a .m file. In the .h you declare what you're going to do and in the .m you do it. So first in the HelloWorldViewController.h type this in...


What were doing here is creating two Interface Builder Outlets. One is a Text Field and were naming that Text Field nameField. Our second outlet is a Label and were naming that nameLabel. Under that we are retaining the properties so we can use them in our app.
The IBAction is an Interface Builder Action that when called upon will do what we tell it.


YOU MUST SAVE YOUR APP before continuing.

Click on HelloWorldViewController.xib to start making our interface.

Drag a label, text field, and round rect button onto your view and position them like this...


Next click on Tools >> Connections Inspector

Make sure the Files Owners is selected and


Connect the Text Field to nameField

Connect the Label to nameLabel

Connect the showName to the button and select touch up inside. We do this because our button will access the (IBAction)showName.

Like This...


When your done the connections inspector will look like this...


Now lets make our view look a little nicer. Double click the button and change the text to "Click Me". After that double click the label and change the text to "Please Enter Your Name". Also drag the ends of the label to the edge of the view. Like This...


While having the label selected go to Tools >> Attributes Inspector and make sure in the layout is selected as center.


Now, save up your Interface Builder and go back to Xcode.

Open up the HelloWorldViewController.m so we can enter in what the showName action will do.

Type in the following code




You must always synthesize what you previously stated in the HelloWorldViewController.h. In our case we have nameLabel and nameField.

Lastly we must define what showName will do. Type in this code...




This code is saying that the text of nameLabel is "Hey %@". %@ is the code for what is typed in the text field. Make sure you close this off with the curly brackets.

Your Done! Click Build & Go and play with your new app.


Stay Tuned to Make an App for more tutorials!





No comments:

Post a Comment