Adding more navigation items

July 30, 2008    Intro Programming

So now that we have a hello world application that works, we want to add more items to the list. I would think most application will not have just one menu item. So here we go.

The easiest way to do this is create an array object that contains all of the menu items. I am not going to go into what everything is just the basic overview. If you want to learn I will point you to the documents on Apple’s website.

If you know C++ this will fit in nicely for you. Open up RootViewController.h and you need to add in the variable for the Array item. So in the following codeblock

@interface RootViewController : UITableViewController {
}

Now you want to add in the Array var so it looks like the following

@interface RootViewController : UITableViewController {
    NSMutableArray      *menuArray;
}

Now save that and open RootViewController.m and look for the following code block

- (void)viewDidLoad {
    // Add the following line if you want the list to be editable
    // self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

Now we want to add in the line to init the array. In c++ this is like initing a new class or php like $class = new Class();

- (void)viewDidLoad {
    // Add the following line if you want the list to be editable
    // self.navigationItem.leftBarButtonItem = self.editButtonItem;

    menuArray = [[NSMutableArray alloc] init];
}

You should now be able to build the application and it will work. Of course we still have only 1 row. So now lets make more. In the same code block as below lets begin to add items into the array.

- (void)viewDidLoad {
    // Add the following line if you want the list to be editable
    // self.navigationItem.leftBarButtonItem = self.editButtonItem;

    menuArray = [[NSMutableArray alloc] init];

    [menuArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                         NSLocalizedString(@"Hello World", @""), @"title",
                         nil,
                         nil]];

    [menuArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                          NSLocalizedString(@"Hello Universe", @""), @"title",
                          nil,
                          nil]];
}

So we have added a Hello World and then a Hello Universe to the array. We are adding a object to the array for later use in the application. an object you can almost look at it like an associative array in PHP.

So remember the number of rows function we changed from 0 to 1? Well if we don’t change that we will never see hello universe, so we have to change that. Bad programming will hard set that to a number like 2. So we want to use the length of the array. This way if we want to add 5 options to the menu, we can just add it to the array and not worry about changing that number at all. So you want that function to look like this now

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [menuArray count];
}

So that will tell the navigation controller to look to show that number of rows.

So now lets look for the cellForRowAtIndexPath function. This function will get called for us 2 times. It will get called as many times as the return value of numberOfRowsInSection tells it to. It will pass in indexPath as the current number. So in the past tutorial we hard coded the Hello World in. So we can remove the following line in that section

[cell setText:@"Hello World"];

Now we want set the text of the current cell to the current array value. So the function should look like this

- (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.text = [[menuArray objectAtIndex:indexPath.row] objectForKey:@"title"];

    // Set up the cell
    return cell;
}

As you can see that is why we used a object inside and array. It is setting the text we set for “title” as the cell text at a certain array value.

Now we want to be a good programming and release the memory that the array took up when we created it. So close to the bottom of the RootViewController.m you will see the following.

- (void)dealloc {
    [super dealloc];
}

Now we want to release the memory so make it look like this

- (void)dealloc {
    [menuArray release];

    [super dealloc];
}

Now when we click build and go we will get the following

Great it worked! You can download the source code here: helloworld2.zip



comments powered by Disqus