这个pushViewController语句如何在我正在做的教程中工作?

时间:2011-04-24 03:31:35

标签: iphone ios pushviewcontroller

我正在从“开始iPhone 4开发”一书中做一个教程,在第09章中他们有以下代码片段:

http://www.apress.com/downloadable/download/sample/sample_id/5/

#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureButtonController.h"

@implementation FirstLevelViewController


@synthesize controllers;



-(void)viewDidLoad{

    self.title=@"First Level";  
    NSMutableArray * array = [[NSMutableArray alloc] init];

    //Disclosure Button 
    DisclosureButtonController *disclosureButtonController = [[DisclosureButtonController alloc]initWithStyle:UITableViewStylePlain];

    disclosureButtonController.title = @"Disclosure Buttons";
    disclosureButtonController.rowImage = [UIImage imageNamed:@"disclosureButtonControllerIcon.png"];
    [array addObject:disclosureButtonController];
    [disclosureButtonController release];

    self.controllers = array;
    [array release];
    [super viewDidLoad];

}


-(void)viewDidUnload{

    self.controllers = nil;
    [super viewDidUnload];
}


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



#pragma mark -

#pragma mark Table Data Source Methods
-(NSInteger) tableView: (UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

    return [self.controllers count];

}


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


    static NSString *FirstLevelCell = @"FirstLevelCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell]autorelease];
    }


    //configure the cell
    NSUInteger row = [indexPath row];
    DisclosureButtonController *controller = [controllers objectAtIndex:row];
    cell.textLabel.text=controller.title;
    cell.imageView.image = controller.rowImage;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;


}

#pragma mark - 
#pragma mark Table View Delegate Methods

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


    NSUInteger row = [indexPath row];

    SecondLevelViewController *nextController = [controllers objectAtIndex:row];
    [self.navigationController pushViewController:nextController animated:YES];


}



@end

最后一句话:

[self.navigationController pushViewController:nextController animated:YES];

对我没有意义,因为基本上你是在推动同一个现有的控制器。但是,当我编译应用程序并运行时,一切正常。我的问题是这个应用程序如何允许我进入下一个屏幕?

1 个答案:

答案 0 :(得分:1)

  

最后一句话对我没有意义,因为基本上你是在推动同一个现有的控制器。

你没有推动同一个现有的控制器,你正在推动来自 controllers 数组的 nextController

  

nextController = [控制器 objectAtIndex:row];

     

[self.navigationController pushViewController: nextController animated:YES];