【问题标题】:How to obtain data passed in performSegueWithIdentifier如何获取performSegueWithIdentifier中传入的数据
【发布时间】:2013-06-11 17:16:43
【问题描述】:

这可能需要两秒钟的时间来回答,但我的搜索技巧在这个问题上并没有让我走得太远。我正在执行 segue,但我不确定如何获取目的地的 id。这是我在 tableview 控制器上的代码。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: NSIndexPath *)indexPath{
    NSLog(@"reaching accessoryButtonTappedForRowWithIndexPath:");
    [self performSegueWithIdentifier:@"leads_calls_to_detail" sender:[[self.leads_calls objectAtIndex:indexPath.row] objectForKey:@"ID"]];
}

我必须在目标视图控制器上创建什么才能获取我试图传递的 id,或者我执行 segue 的方式与我正在尝试的不兼容?

【问题讨论】:

  • 需要实现一个prepareForSegue,其中可以参考segue.destinationController

标签: ios objective-c cocoa-touch segue


【解决方案1】:

您应该将值传递给 prepareForSegue: 内的 destinationViewController 并将 self 作为发件人传递..尝试使用类似的东西:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"leads_calls_to_detail"])
    {
       YourViewController *controller=(YourViewController *)segue.destinationViewController;
       NSIndexPath *path = [self.tableView indexPathForSelectedRow];
      //Or rather just save the indexPath in a property in your currentViewController when you get the accessoryButtonTappedForRowAtIndexPath callback, and use it here
       controller.yourPropertyToSet = [self.leads_calls objectAtIndex:path.row];
    }
}

另外,根据 Rob Mayoff 的说法,您可以使用以下方式获取附件被点击的行的索引路径:

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];//where sender is the sender passed in from prepareForSegue:

How to find indexPath for tapped accessory button in tableView

【讨论】:

  • 谢谢!我想我可以从这里弄清楚! :)
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 2019-03-17
  • 2018-02-26
  • 1970-01-01
  • 2012-03-03
  • 2016-01-10
  • 2018-03-11
相关资源
最近更新 更多