Getting started with iOS

This is a quick an simple guide to writing code for iOS. This is a very simple Hello World application that is all written programmatically. This means no using the Storyboard or Interface Builder.

It always good to learn how to program without using the visual aids. As good as Storyboard and Interface Builder are being able to code without the need to use them is really useful and enables you to do a lot more advanced coding.

Starting with XCode create a new project and Select single view application

You will not need the ViewController.xib so you can delete this. Open the AppDelegate.m

Change the line:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

and replace it with
self.viewController = [[ViewController alloc] init];

This will stop the application from looking for the xib (Interface builder file).

Open ViewController.m then in -(void)viewDidLoad firstly we will set a background colour for the view. For this we will set it as a standard blue colour.

self.view.backgroundColor = [UIColor blueColor];

Now we need to create a label to say Hello World. We will call this label HelloLabel. We will put this label 10 pixels in from the left and 100 pixels down from the top. We will make this label 300 pixels wide and 25 pixels high.

UILabel *HelloLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,100,300,25)];

Now we will set the text to say Hello World, set the colour to white and its text alignment to centre. We will also remove any background colour from the label so that the view background colour shows through. Finally we will set the font size to 20 pixels.

HelloLabel.text = @"Hello World";
HelloLabel.textColor = [UIColor whiteColor];
HelloLabel.textAlignment = NSTextAlignmentCenter;
HelloLabel.backgroundColor = [UIColor clearColor];
HelloLabel.font = [UIFont systemFontOfSize:20];

Finally we need to add the label to the view so that it becomes visible.

[self.view addSubview:HelloLabel];

Now when you run the application you will see a blue screen with a label saying Hello World in white text.

Why not try playing around with different colour or font sizes. You could also try changing the positioning of the label or the text it displays.