How can this pushViewController statement work in the tutorial I am doing?
I am doing a tutorial from the book "Beginning iPhone 4 Development" and in Chapter 09 they have the following snippet of code:
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开发者_如何学Python 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
The very last statement:
[self.navigationController pushViewController:nextController animated:YES];
makes no sense to me because essentially you are pushing the same existing controller. However when I compile the app and run, everything works just fine. My question is how is this app allowing me to go to the next screen?
The very last statement makes no sense to me because essentially you are pushing the same existing controller.
You're not pushing the same existing controller, you're pushing the nextController, which comes from the controllers array:
nextController = [controllers objectAtIndex:row];
[self.navigationController pushViewController:nextController animated:YES];
精彩评论