2013-10-08 34 views
0

對於我的UITableView中的每個單元,我設置了一個自定義對象。像這樣:如何將對象從單元傳遞到新控制器

// Reference to the turn 
PFObject *turnToPlay = [self.currentUserTurnsToPlay objectAtIndex:indexPath.row]; 

當選擇一個單元格時,我需要將該對象傳遞給一個新的視圖控制器。

這是從我的didSelectRowAtIndexPath

if (indexPath.section == 0) { 
    [self performSegueWithIdentifier:@"takeGameTurn" sender:self]; 
} 

我很努力找出我怎麼能引用到該小區創建了PFObject,將它傳遞到目的地視圖控制器。我明白我會通過這個prepareForSegue,但我如何從單元格選擇中獲得我的對象。

我引用了didSelectRowAtIndexPath的indexPath,但是如何將它傳遞給segue目標控制器?這就像我需要另一個參數。

回答

0

在你prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     if ([segue.identifier isEqualToString:@"takeGameTurn"]){ 
       NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
       PFObject *turnToPlay = [self.currentUserTurnsToPlay objectAtIndex:indexPath.row]; 

      } 
    } 
+0

謝謝,需要indexPathForSelectedRow,不知道這是可用的。 – StuartM

0

是你的你的PFObject屬性?如果是的話,你可以使用

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"takeGameTurn"]) 
    { 
     YourNextViewController *nextVC = [segue destinationViewController]; 
     NSIndexPath *indexPath; 
     indexPath = [myTableView indexPathForSelectedRow]; 
     MyCustomCell *cell = (MyCustomCell *)[myTableView cellForRowAtIndexPath:indexPath]; 
     nextVC.myPFObject = cell.assignedPfObject; 


    } 
} 
相關問題