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

发布于 2024-11-03 08:32:17 字数 2711 浏览 3 评论 0原文

我正在做《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-11-10 08:32:17

最后一句话对我来说毫无意义,因为本质上你是在推动相同的现有控制器。

您不是推送同一个现有控制器,而是推送 nextController,它来自 controllers 数组:

nextController = [控制器 objectAtIndex:row];

[self.navigationController PushViewController:nextController 动画:YES];

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];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文