【问题标题】:How to pass an object from cell to new controller如何将对象从单元格传递到新控制器
【发布时间】:2013-10-08 16:19:42
【问题描述】:

对于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 目标控制器?就像我需要该方法中的另一个参数。

【问题讨论】:

    标签: ios uiviewcontroller uitableview uistoryboardsegue


    【解决方案1】:

    在你的 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];
    
              }
        }
    

    【讨论】:

    • 谢谢,需要 indexPathForSelectedRow,不知道有没有。
    【解决方案2】:

    你的单元格是你的 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;
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2023-04-04
      • 2015-10-07
      • 2017-10-22
      • 1970-01-01
      相关资源
      最近更新 更多