2012-09-01 31 views
2

所以我有didSelectRowAtIndexPath方法成爲UITableViewController發送indexPath.row到另一個UIViewController

TableView轉到UIViewController,它由NavigationController控制。

我想發送indexPath.row由NavigationController稱爲類,是有什麼辦法發送參數到另一個類

我的應用方案是:

TabBarController ---> NavigationController ---> TableViewController --->的UIViewController

回答

0

如果您使用的故事板:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UITableViewCell *cell = sender; 
    // Make sure your segue name in storyboard is the same as this line 
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"]) 
    { 
     // Get reference to the destination view controller 
     YourViewController *vc = [segue destinationViewController]; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

     // Pass any objects to the view controller here, like... 
     [vc setIndexPath:indexPath]; 
    } 
} 

其他

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) { 
     YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; 
     [vc setIndexPath:indexPath]; 
    } 

} 
+0

我在使用storyboard:我必須調用prepareForSegue方法嗎?我想在didSelectRowAtIndexPath – Wildchild89

+0

而且..我在initWithNibName中調用的UIViewController不是一個TableViewControll,所以它沒有setIndexPath選擇器。 – Wildchild89

+0

如果您從故事板中的tableViewCell中拖動segue,您不需要調用prepareForSegue你的viewController,你必須將它命名爲「YOUR_SEGUE_NAME_HERE」,然後當用戶點擊單元格時,調用prepareForSegue方法。如果你想傳遞數據,你需要在你的UIViewController中有一些@property,例如NSIndexPath或NSInteger行,當你傳遞set屬性[vc setRow:indexPath.row]; – mientus