UINavigationController,UIBarButtonItem使用的例子

UINavigationController,UIBarButtonItem应用的例子

rootViewController.m文件

- (void)viewDidLoad
{
    self.title = @"First Page"; 
    NSMutableArray *array = [[NSMutableArray alloc] init];
    DisclosureButtonController *buttonController = [[DisclosureButtonController alloc] initWithStyle:UITableViewStylePlain];
    buttonController.title = @"Button View";
    buttonController.image = [UIImage imageNamed:@"apple.png"];
    [array addObject:buttonController];
    
    self.listData = array;
    [array release];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"主页" style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonPressed)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStyleBordered target:self action:@selector(rightButtonPressed)];
    [super viewDidLoad];
}

- (void)leftButtonPressed
{
    NSLog(@"Lefl Button");
}

- (void)rightButtonPressed
{
    NSLog(@"Right Button");
}
#pragma mark -
#pragma mark Table View Data Source Methods

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *thekey = @"thekey";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:thekey];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:thekey
                ];
    }
    NSUInteger row = [indexPath row];
    secondLevelViewController *nextController = [listData objectAtIndex:row];
    cell.textLabel.text = nextController.title;
    cell.imageView.image = nextController.image;
    return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryDisclosureIndicator;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    secondLevelViewController *nextController = [listData objectAtIndex:row];
    [self.navigationController pushViewController:nextController animated:YES];
}

 DisclosureDetailController.m文件

- (void)viewDidLoad
{
    label.text = message;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleBordered target:self action:@selector(rightMethods)];
}

- (void)rightMethods
{
    NSLog(@"...........");
}

 DisclosureButtonController文件

- (void)viewDidLoad
{
    NSArray *array = [[NSArray alloc]initWithObjects:@"The First One",@"The Second One",@"The Thr One",@"The Fou One", nil];
    self.list = array;
    [array release];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"last Page" style:UIBarButtonItemStyleBordered target:self action:@selector(uppage)];

}

- (void)uppage
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

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

#pragma mark -
#pragma mark Table View Data Source Methods

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *theNewKey = @"theNewKey";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theNewKey];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:theNewKey] autorelease];
    }
    NSUInteger row = [indexPath row];
    NSString *theString = [list objectAtIndex:row];
    cell.textLabel.text = theString;
    
    return cell;
}


#pragma mark -
#pragma mark Table Delegate Methods

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryDetailDisclosureButton;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Waring" message:@"You Pressed" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if(detailController == nil){
        detailController = [[DisclosureDetailController alloc] initWithNibName:@"DisclosureDetail" bundle:nil];
    }
    
    detailController.title = @"The Detail View";
    
    NSUInteger row = [indexPath row];
    
    NSString *warItem = [list objectAtIndex:row];
    NSString *detailSelect = [[NSString alloc] initWithFormat:@"You Pressed Is %@",warItem];
    detailController.message = detailSelect;
    [detailSelect release];
    [self.navigationController pushViewController:detailController animated:YES];

}