Show Menu

Do you have a job opening?

Submit your 30 day Job Listing for FREE

Follow along this Objective-C Tutorial to create your first iOS 7 Hello World iPhone Application.

In this iPhone development tutorial, We are going to create a basic iOS 7 application which will take the users inputed name from the UITextField and display it in a UILabel. We will then also use the Objective-C isEqualToString method to check whether the inputed name is the same as mine. This will all happen when the user clicks on a UIButton which will trigger an IBAction.

If you would like to follow this tutorial on video format please view it below. At the bottom of the tutorial you can also download the source files for this project.

If you cannot see this video in your browser, please find it on YouTube .

Lets get started

The first thing you need to do is create a new Xcode Single View Application:

Xcode Single View Application

Then click the next button at the bottom right of this popup, you will then be presented with another screen asking you to chose options for your application. I am going to aptly name this project: HelloWorld. For the purpose of this project the organisation name can be anything you want and I have used com.ios-blog as the company identifier, but again this can be anything that you want.

We now need to edit your iOS Application implementation file which is the viewController.m and add two properties. The first @property will be our UILabel which will be the label where the Hello World or Hello Name message will appear. The second @property will be the UITextField, which will be where the user enters their name.

So, just under the @interface ViewController() add:

@property (nonatomic, weak) IBOutlet UILabel *messageLabel;
@property (nonatomic, weak) IBOutlet UITextField *inputField;

As I mentioned earlier, there will be a UIButton which triggers a IBAction. This is where the rest of our code will go and ensure that it only runs when the user clicks enter.

Just under @implementation ViewController type the start of our IBAction which we will call Enter:

-(IBAction)Enter {

Inside this IBAction we now need to declare three NSStrings:

NSString *yourName = self.inputField.text;
NSString *myName = @"Mark";
NSString *message;

The first NSString (NSString *yourName = self.inputField.text;) will get the value that is entered into the UITextField (inputField) and assign it to the variable: yourName. The second NSString declared the variable myName with the value of Mark. The last NSString just declares the variable message ready to use.

We now need to create an Objective-C if statement. The reason we are doing this particular if statement is so that our app doesn’t crash when we click the enter button without first typing any value into the UITextField:

if ([yourName length] == 0) {
  yourName = @"World";

This basically says that should the user not enter a value into the UITextField before they click the enter button, then assign the value of World to the variable yourName.

Now, lets check whether the entered name is the same as my name. For this we need to use the Objective-C method isEqualToString. We do this by comparing the two variables yourName and myName. If the names are the same then we need to add a value to the NSString message variable we defined earlier. If the entered name is not the same then we just need to output the basic message of ‘Hello youName’. For this we declare an Objective-C if-else statement:

if ([yourName isEqualToString:myName]) {
        message = [NSString stringWithFormat:@"Hello %@ We have the same name", yourName];
}else {
        message = [NSString stringWithFormat:@"Hello %@", yourName  ];

Once we have acquired and defined the variables we need to use we then need to make sure that they appear in the UILabel. for this we need to add one more line of code inside our IBAction:

self.messageLabel.text = message;

That’s it for the coding part. However we still need to add the elements and assign then correctly so that the application knows what it is doing. In your Project Navigator to the left, click Main.storyboard. Once this has loaded you need to drag in three elements from your Objects library in the bottom right of Xcode. The elements that you need are: UILabel, UIbutton, UITextField.

xcode object library

At the moment your UILabel says Label and your UIButton says Button. Double click your button and change the text to Enter and do the same for the UILabel however, remove all the text completely. Your storyboard should now look like:

xcode main storyboard hello world

In my example you will notice that the UITextField has what is known as a placeholder of “Enter your name”. To do this you need to head to the attributes inspector which is located top right of Xcode:

xcode attributes inspector placeholder


Lastly we now need to link up our elements to the code that we have just created. Click the UIButton and hold down Ctrl at the same time, drag your mouse down to the View Controller button at the bottom of your storyboard.

xcode files owner

When you release you will be presented with a few options, Click Enter. Thats it you have successfully linked them up. Do the same for the other two elements but make sure that you link the UILabel to message and UITextField to yourName.

xcode files owner enter

That’s it. Hit run and play with your application. Feel free to download the source code below.

The source files

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

iOS-Blog Admin Team

Written by:

We're here to help.