views:

34

answers:

1

I have created the view Controllers and attached those to control their respective views. I can't figure out how to make the following code to go the right views though. (This is in the root view controller:)

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 RunViewController *runViewController = [[RunViewController alloc] initWithNibName:@"RunView" bundle:[NSBundle mainBundle]];
 CalcViewController *calcViewController = [[CalcViewController alloc] initWithNibName:@"CalcView" bundle:[NSBundle mainBundle]];
 PushViewController *pushViewController = [[PushViewController alloc] initWithNibName:@"PushView" bundle:[NSBundle mainBundle]];
 SitViewController *sitViewController = [[SitViewController alloc] initWithNibName:@"SitView" bundle:[NSBundle mainBundle]];
 TimerViewController *timerViewController = [[TimerViewController alloc] initWithNibName:@"TimerView" bundle:[NSBundle mainBundle]];

 [self.navigationController pushViewController:runViewController animated:YES];
 [runViewController release];
 [self.navigationController pushViewController:pushViewController animated:YES];
 [pushViewController release];
 [self.navigationController pushViewController:sitViewController animated:YES];
 [sitViewController release];
 [self.navigationController pushViewController:timerViewController animated:YES];
 [timerViewController release];
 [self.navigationController pushViewController:calcViewController animated:YES];
 [calcViewController release];

 runViewController = nil;
 pushViewController = nil;
 sitViewController = nil;
 timerViewController = nil;
 calcViewController = nil;


}

Each time I select any of the rows in the table - all of the views come up. How do I arrange them to only open the view that it is supposed to?

A: 

The problem is that you are calling pushViewController:animated on every view controller. If you only want one of the view controllers to come up just call pushViewController:animated only on the one you want. Here's a sample:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath*)indexPath {

    // This code shows a different view depending on the selected row.
    UIViewController *viewController = nil;
    switch (indexPath.row) {
        case 0:
            viewController = [[RunViewController alloc] initWithNibName:@"RunView" bundle:[NSBundle mainBundle]];
            break;

        case 1:
            viewController = [[CalcViewController alloc] initWithNibName:@"CalcView" bundle:[NSBundle mainBundle]];
            break;

        case 2:
            viewController = [[PushViewController alloc] initWithNibName:@"PushView" bundle:[NSBundle mainBundle]];
            break;

        case 3:
            viewController = [[SitViewController alloc] initWithNibName:@"SitView" bundle:[NSBundle mainBundle]];
            break;

        case 4:
            viewController = [[TimerViewController alloc] initWithNibName:@"TimerView" bundle:[NSBundle mainBundle]];
            break;
    }

    if (viewController != nil) {
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }

}

Mike H.