Step 6: Have some fun by adding things programmatically

Picture of Have some fun by adding things programmatically
Screen Shot 2012-08-15 at 3.47.58 PM.png
Screen Shot 2012-08-15 at 3.48.18 PM.png
To make it a little more advanced, we are going to add everything programmatically.

1. Delete the label we added to the UI as well as all the code we wrote up to this point.

2. Open ViewController.h and add the Following code between the @interface ViewController: UIViewController and @end:
@property (strong, nonatomic)UILabel *label;
Notice how a circle did not appear this time? That's an indication that you did it correctly

3. In ViewController.m add @synthesize label; right beneath @implementation ViewController and add [self setLabel:nil]; in the viewDidUnload function.

4. In the viewDidLoad function add the following lines of code:
//Define where the label will be displayed
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
//Define the text to be displayed
self.label.text = @"Hello World";
//Center the Text
self.label.textAlignment = UITextAlignmentCenter;
//Programmatically add the label to the view
[self.view addSubview:self.label];

5. Hit run and admire your handiwork, you've completed your first iOS application
gumption2 years ago
Thanks for the simple introduction to iOS app development.

FWIW, an alternate approach to the proposed substitution above would be to add another label.

Since I'm using iOS 6, UITextAlignmentCenter is deprecated; a post on StackOverflow (http://stackoverflow.com/questions/12792999/what-are-good-alternatives-to-uitextalignmentcenter-in-ios-6) suggested using NSTextAlignmentCenter instead ... and for backward compatibility, to use the following:

#ifdef __IPHONE_6_0
# define ALIGN_CENTER NSTextAlignmentCenter
# define ALIGN_CENTER UITextAlignmentCenter

I added the following line to ViewController.h (below the @property declaration for label):

@property (strong, nonatomic)UILabel *label2;

and added the following line in ViewController.m (below the @synthesize label; line):

@synthesize label2;

After adding the preprocessor macro proposed in the StackOverflow, and using the code provided above (substituting "label2" for "label" in the added statements), my viewDidLoad method appears as follows:

- (void)viewDidLoad
    [super viewDidLoad];

    // from before:
    self.label.text = @"Hello World!";

    // using a Y coordinate of 200 instead of 0 for label2
    self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 100)];
    self.label2.text = @"Goodbye";
    self.label2.textAlignment = ALIGN_CENTER;
    [self.view addSubview:self.label2];
hojijoon3 years ago
very nice thank you bro!
carlmal3 years ago
Would be great if you could include how to get the app onto your device. Thanks for the instructable.
zacker3 years ago
all that just to say Hello World? whats it take to make an actual app like these photo editing apps and such?
remendab zacker3 years ago
That is not all that much work - really. With much power comes much responsibility, meaning that there are many small things required just to get up and running, but once you are, running that is, the next steps are not that much more complex
zacker remendab3 years ago
...like baby steps.. lol Thanks!
Very nice introduction to writing an app! This could be the road to riches for someone with a good idea an a willingness to learn.

Thank you!!