如何将从didSelectRowAtIndexPath中选择的行数传递给initWithNibName?

问题描述:

我有一个tableView(第一视图)和detailView(下一个视图)包含该表的详细信息。如何将从didSelectRowAtIndexPath中选择的行数传递给initWithNibName?

现在,从第一个视图中选择一个特定的行,我希望将所选行的索引号从第一个视图的didSelectRowAtIndexPath传递到下一个视图的initWithNibName。我怎样才能做到这一点?

您可以在您想要实例化的视图控制器中实现自己的方法,并调用该方法而不是-initWithNibName:bundle:。在该方法中,您可以拨打

// in your view controller 
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle index:(NSInteger)index { 
    [self initWithNibName:nibName bundle:nibBundle]; 
    // do your stuff with the index here 
} 

// in your table view delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    DetailView *vc = [[DetailView alloc] initWithNibName:@"YourNibName" bundle:nil index:indexPath.row]; 
    [self.navigationController pushViewController:vc animated:YES]; 
    [vc release]; 
} 
+0

感谢您所需要的。 :) – Zaraki 2011-02-10 11:47:58