如何获得segue标识符

时间:2011-12-16 23:57:42

标签: ios cocoa-touch uistoryboardsegue

我很高兴,在我开始之前就这么说。

我要做的是制作一个单元格表,当触摸单元格时,它会根据触摸的单元格显示图像。现在,我认为有一种方法可以判断触摸了哪个单元格并相应地设置了UIImageView图像,或者我将拥有与表格单元格一样多的视图并且有大量的segues?基本上我要做的是获取segue标识符并使​​用它来设置UIImageView图像。

2 个答案:

答案 0 :(得分:6)

您可以使用此方法获取segue标识符,如果按下segue则调用该方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"YourChoiseMadeInStoryboard"]) {
  ...
  //to access the destination ViewController do this
  AViewControllerClass *viewController = segue.destinationViewController;
  }
}

答案 1 :(得分:1)

UIStoryboardSegue旨在促进UIViewController的两个实例之间的转换。你要做的事情根本不需要segue。您想要在单击时切换单元格上的图像视图。您只需要在-tableView:didSelectRowAtIndexPath:上实施UITableViewDelegate方法。

修改我假设您的UIImageView名为imageView已经有了ivar。您还需要一个包含所有图像的NSArray的ivar。每个索引处的图像将对应于表中的每一行。在-viewDidLoad之类的地方建立你的数组,并假设它被称为imageArray

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get the cell at the selected index path
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    // After being tapped, set the image view to some image
    self.imageView.image = [self.imageArray objectAtIndex:indexPath.row];
}