Creating Hello World

July 30, 2008    Intro Programming

So like every good tutorial we need to create a Hello World application. We will use what we have learned in the past tutorials and build on them.

So the first thing you want to do is open the file RootViewController.m

Look for the following code block

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

We want to make it return a 1 so make it look like the following

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

If we return 1 then it means we will have 1 row in the navigation view.

So now look for the following code block

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    // Set up the cell
    return cell;
}

Now we want to give the row some text. The one thing I really like about ojective C so far is how they handle getting and setting vars in a class.

So add [cell setText:@”Hello World”];

Here is the result

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    [cell setText:@"Hello World"];

    // Set up the cell
    return cell;
}

Now when we save the file and click Build and Go we should get the following.

So you have just completed the Hello World application. If you want to download the source grab it here. Helloworld.zip

I hope this helps!



comments powered by Disqus