iOS Objective-C basics: Build Your First App

Thursday Apr 26th 2012 by Laurence Moroney

Laurence Moroney shows you how to launch your first application and how to build a slightly more complex 'Hello World' application that processes user input.

In the first article of this series, you saw how to get Xcode and use it to build your first, simple, 'Hello World' application. In this article, you'll expand on that, learning how the application is launched, as well as building a slightly more complex application that processes user input.

If you haven't done so already, download and install Xcode from the app store. Once you've done that, create a new single view iOS application called 'Hello World'.

Starting the App

Last time you saw that AppDelegate had the functions that manage the application lifecycle, and ViewController has the user interface definitions and logic. Before going further, there's one more file you should look at, and it's hidden in the 'Supporting Files' folder. It's called main.m, and if you are familiar with C programming, it'll be immediately familiar.


The main function is the first function called by the operating system when an app starts. It can take parameters (also called arguments), which are passed in as a single argument (argc), followed by one or more further arguments (argv, received as an array).

It then creates a new UIApplicationMain class from these, using the AppDelegate as the definition for the application. While you likely won't edit any of this stuff directly, it's useful to know about the app lifecycle with main.m to start, and AppDelegate to define.

Properties, Methods and Events

In object oriented programming, your objects are defined using classes. For example, my car is an object, which is an instance of a Toyota Camry class. You program your objects using properties, methods and events.

A property is something that defines an attribute of your object. Following the car example, it's color is a property.

A method is an action that you perform on your object. In the example of a car, a method might be to turn the key in the ignition.

An event is something that happens on the object that you might want to respond to. An example of this with a car is that the fuel indicator light comes on if you are low on gas, and you'll then perform the method of filling the gas tank in response.

In this section you'll look at some of the controls used to create a user interface on the iPhone, and use their properties, methods and events to program them.

Your App

In the previous article, you had a single Label control that read 'Hello, World'. In this article, you'll expand on that to have a text input box in which the user can type something. The label will then be updated to say 'Hello, World - ' plus whatever was typed in. The code to define it should be in your ViewController.m file, and look something like this:

ViewController.m file
ViewController.m file

(Refer back to the previous article to learn about the view controller and adding subviews)

This code does the following:

  • Define 4 User Interface controls:
  • A label for the prompt
  • A label containing 'Hello World :' plus whatever the user types
  • A button that when tapped will update the hello world label
  • A textview that is used to type in the text

In the ViewDidLoad function, each of these controls is initialized and their properties are set. The properties include the Frame, which defines the rectangle in which the control will be drawn and the default text for the control amongst others.

The only event that we want to capture is when the user taps the button. This event is called UIControlEventTouchUpInside. We use the method called addTarget to specify that the event should be caught and handled by a function called updateHW.

It's a good rule of thumb to think of properties being set using 'dot' syntax (i.e. txtInput.text), and methods being called using 'bracket' syntax (i.e. [btnGo addTarget]), but there is some overlap between the two.

The event handler function was defined as updateHW, so this needs to be implemented, and you can see it implemented here:

Event Handler Function Defined as updateHW
Event Handler Function Defined as updateHW

This simply reads the contents of txtInput and loads them into a string. A new string is then created using the format 'Hello World: %@'. The %@ is a parameter defining a string that is simply replaced using the variable specified.

So, if you type 'Laurence' into the textbox and press the button, the 'name' variable will be Laurence, and 'Hello, World :%@' will become 'Hello, World :Laurence'.

This will then be loaded into the hello world label, the results of which are here:

Textbox Variable Loaded Into Label 
Textbox Variable Loaded Into Label

Note that the labels have a white background because the background color property hasn't been set. See if you can fix it to make them transparent. (Hint, use UIColor clearColor)...

So now you've created your first interactive application -- where you've covered all the basics of rendering controls, capturing user input, and updating the controls as a result!

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved