Pages

Wednesday, September 1, 2010

iPhone SQLite Tutorial Part 2

This follows on directly from iPhone SQLite Tutorial Part 1. Complete that tutorial before beginning this one.

Show Description of our Heroes

1. We are going to display a description of the hero when the his row is touched. For that we need to display another view with a UITextView. Open HeroViewController.h and add a variable of type UITextView:
@interface HeroViewController : UIViewController {
    UITextView *heroDesciption;
}

@property (nonatomic, retain) IBOutlet UITextView *heroDesciption;

@end

2. Next modify the HeroViewController.m file to synthesize heroDescription.

3. Now we modify the view HeroViewController.xib. Drag a UITextView to the View Window. Then connect up the UITextView to our heroDescription.

4. Lastly we are going to implement the behaviour that will display the HeroViewController view.

Open RootViewController.m. We uncomment ViewDidLoad method and add a title for the view:
self.title = @"Avengers";

Then we edit the didSelectRowAtIndexPath like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AvengersDatabaseAppDelegate *appDelegate = (AvengersDatabaseAppDelegate *)
                                                [[UIApplication sharedApplication] delegate];
    Hero *hero = (Hero *)[appDelegate.Heroes objectAtIndex:indexPath.row];
    
    HeroViewController *heroViewController = [[HeroViewController alloc] 
                                              initWithNibName:@"HeroViewController" bundle:nil];

    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:heroViewController animated:YES];
    heroViewController.title = hero.name;
    heroViewController.heroDesciption.text = hero.desc;
    [heroViewController release];

}

And you are done. Save and run the application. Clicking on a name should slide a new view in that displays a description of the superhero you clicked on.

No comments:

Post a Comment