Find any tutorial you need, from website design to Advanced coding, we will find the best tutorials for you.

IOS Hello World

You now should have the iPhone sdk installed and Xcode open.

I am going to show you two ways to make this application.

The first way

The first way is all code, you will not be using interface Builder. First, Create a new application (on the Xcode start page click on View-Based Application) Call it "Hello World" and make sure the device is set to iPhone Next, open classes in the sidebar. Click on "helloWorldViewController.m". You will notice alot of green text(Green text represents comments, comments are marked by '// Comment' or '/* Comment */', I wrote comment to show you where the text goes) Scroll down until you see viewDidLoad, uncomment the text by deleting the "/*" on top and the "*/" below. Now the fun bit, Copy the code below into the viewDidLoad,(Do not touch the [super viewDidLoad])

The code


- (void)viewDidLoad {

    [super viewDidLoad];

	int w = self.view.frame.size.width;

	int h = self.view.frame.size.height;

	UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

	label.center = CGPointMake(w/2, h/2);

	label.textAlignment = UITextAlignmentCenter;

	label.backgroundColor = [UIColor clearColor];

	label.text = @"Hello World!";

	[self.view addSubview:label];

	[label release];

}
Once copied click save then build and run, the iphone simulator will open to show you your application So let me just explain what is happening above. int is calling a new integer (an integer is a number eg. 1, 2, 3 and -1, -2, -3) the two lines tell the iPhone that w is for the width of the frame and h is for the height. UILabel tells the iPhone that there is a new label. The code created the size of the label with "initWithFrame:CGRectMake(0, 0, 100, 50)". label.center calculated the center of the screen to center the label, notice w/2 and h/2. The next two are very straight forward, backgroundColor and text. [label release] is used to give the memory back to the iPhone cpu. Please note that the iPhone does not have a garbage collector and you will have to organise memory yourself. For beginners, a garage collector organise the code and memory to work together. If you do not organise the memory correctly, the application will end.

The second way

The second way is by far the easiest to use, we will be using the interface builder to create the same effect First, open up "resources" in the sidebar. Double click the helloWorldViewController.xib, this will open the interface Builder Next, drag the label over to the window and place it in the center like the image above, rename the label by double clicking it and typing "Hello World!" The label is found in the library window, scroll until you see it. Next click on the label and go to the inspector(The inspector is a tall window with four tabs at the top) Click on the tab which has a picture of a ruler. Scroll down to the header placement, click the two icons below to center the label. Save and then go back to Xcode. Then go to "helloWorldViewController.h" in classes. Place the following code inside the brackets: IBOutlet UILabel* myLabel; Next, open the helloWorldViewController.xib again, Now open the connections inspector by going to tools > connections Inspector The Connections Inspector is inside the tall window with the four tabs on the top. Click and hold on the small circle and drag the line to the files owner(shaped like a cube) and click on mylabel Save and quit the interface Builder, go to Xcode and click on Build and Run.

Related

1 Comment

Add a comment

Please login to comment

Click here to login