ios - 在 UICollectionViewController 中按下 UICollectionViewCell 后无响应

标签 ios objective-c uiviewcontroller uicollectionview segue

当我将图像从 UICollectionViewController 发送到另一个 ViewController 时,这里是 View A:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString:@"Detail Segue"])
     {
        PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
        NSIndexPath *path = [[self.collectionView indexPathsForSelectedItems]  lastObject];

        Photo *selectedPhoto = self.photos[path.row];
        PhotoDetailVC.photo = selectedPhoto;
    }
}

我在 ViewController( View B)中使用以下方式接收它:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.imageView.image= self.photo.image;
}

最佳答案

我认为问题在于您没有在实际负责触发导航的代码中的任何地方调用 performSegueWithIdentifier:sender: 而不是 prepareForSegue:sender: 这是准备导航时调用

所以我建议实现 collectionView:didSelectItemAtIndexPath: 如下

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    Photo *selectedPhoto = self.photos[path.row];
    // this line is the one that is responsible for navigation
    [self performSegueWithIdentifier:@"Detail Segue" sender:selectedPhoto];
}

并修改prepareForSegue:sender:如下

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"Detail Segue"]) {
        PhotoDetailViewController *PhotoDetailVC = segue.destinationViewController;
        PhotoDetailVC.photo = sender;
    }
}

关于ios - 在 UICollectionViewController 中按下 UICollectionViewCell 后无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064333/

相关文章:

ios - Facebook SDK 3.1 - 发送应用请求

ios - MQTT 通过多个 ViewController 发送消息

ios - Stackmob 不会在没有任何错误的情况下执行提取请求

objective-c - NSMapTable 和 NSMutableDictionary 的区别

ios - 如何从 NSObject 类显示 UIViewController?

ios - 从容器 View 获取数据

ios - 透明的 iOS 导航栏

ios - 在 opencv ios 中检测 C & CXX 编译器错误

ios - 如何在应用程序加载前将我的 iPad 设置为横向模式?

ios - 如何禁用 UIButton 色调颜色?