这个pushViewController语句在我正在做的教程中如何工作?
我正在做《Beginning iPhone 4 Development》一书中的教程,在第 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];
对我来说毫无意义,因为本质上你是在推动相同的现有内容控制器。但是,当我编译应用程序并运行时,一切正常。我的问题是这个应用程序如何允许我进入下一个屏幕?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不是推送同一个现有控制器,而是推送 nextController,它来自 controllers 数组:
You're not pushing the same existing controller, you're pushing the nextController, which comes from the controllers array: