Hello World App in iPhone / iOS / Objective-C using XCode

Hi Friends,

If you need a sample app to execute in XCode developer Tool to launch your application in iPhone / iPad simulator, this post will help you to.

Following is the environment using which I’ve created my Hello World application. If you need to develop in any other environment still I hope this will help you. There will be a light modification in Menu options and Categories in IDE. If you still have issues, please free to post your queries.

OS : Mac OS 10.7.5

XCode IDE : 4.6

iPhone / iPad Simulator : 6.1

Before creating your first iOS application, please have a look on “An overview of iPhone / iPad / iOS / Objective-C app“.

1. Open XCode Tool. It prompts you either to create a new project or to create from repository. Select, “Create a new Xcode project”.

Create new Xcode project

Create new Xcode project

2. Select “Single View Application” from Application Tab under iOS section.

Select View Applicaton

Select View Application

3. Enter all the details in the succeeding dialog box that appears.

Product Name : Equivalent to project name that we create in Eclipse

Organization Name : Optional. When you are creating a class, the comment will be created with this name. For example,¬†Copyright (c) 2014 Anitha Rosary. All rights reserved.”

Company identifier : this is equivalent to your package name that we create in Eclipse (For Example, com.photon)

ClassPrefix(optional) : This name will be prefixed for all the classes by default.

Devices : Select the preferred device iPhone / iPad / Universal. Universal will support both the devices.

Product details

Product details

Click Next and select the location where the product has to be created.

Once the product has been created, your project structure will look like this.

Product Directory Structure

Product Directory Structure

4. Now, declare the label variable and create the interface to display the string when button is clicked in HelloWorldViewController.h

//
//  HelloWorldViewController.h
//  HelloWorld
//
//  Created by Anitha Rosary on 17/04/14.
//  Copyright (c) 2014 Anitha Rosary. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HelloWorldViewController : UIViewController{
    IBOutlet UILabel *label;
}
- (IBAction)button;
@end

Now implement your action in HelloWorldViewController.m:

//
//  HelloWorldViewController.m
//  HelloWorld
//
//  Created by Anitha Rosary on 17/04/14.
//  Copyright (c) 2014 Anitha Rosary. All rights reserved.
//

#import "HelloWorldViewController.h"

@implementation HelloWorldViewController

- (IBAction)button {
    label.text = @"Congrats Anitha...!!!";
}

@end

Save your changes by pressing “Command + S”.

5. Let us start with view design. For our application, we need a label to display the string and a button to perform the action. Open “HelloWorldViewController.xib” file.

xib file

xib file

Label and buttons will be available in Object Library. After placing label, remove the label text using Attribute inspector by modifying “Text” attribute for Label object. After placing button, modify the text in “Title” attribute.

6. If you have followed all the above steps, click on File’s Owner under Place Holders section. Select Connection Inspector and check whether outlets are loaded with “label” and “view” under Outlet section and “button” under Received Actions section.

  • Drag “+” mark that appears near label and drop it in label
  • Drag “+” mark that appears near view and drop it in view (be careful entire view has to be selected and not any label or button ie, objects in view)
  • Drag “+” mark that appears near button and drop it in button. After doing this, a dialog box appears with actions, select “Touch up inside” to perform the action when user taps / double taps / clicks on the button.

Now it should appear like below:

View Controller Connection Inspector

View Controller Connection Inspector

Now we have done with the development and design. Save the application.

Run the application using Run button or go to Product -> Run.

Your application has to be launched either in iPad / iPhone simulator.

iPhone / iPad Simulator 6.1

iPhone / iPad Simulator 6.1

Thanks guys!

If you face any issues, please post your queries.

Share This Post

Related Articles

Leave a Reply

© 2017 Techy Diary. All rights reserved.
Powered by Charvi Groups