选定的图像不会出现在详细视图控制器中

时间:2015-04-21 09:39:03

标签: ios objective-c uicollectionview

我在viewcontroller中有一个CollectionView。我把这个viewController命名为TabViewController。现在在该集合中我有一些图像。现在,当我点击单元格时,它将推送到另一个名为DetailViewController的viewController。在DetailViewController中我有一个imageView。我想在DetailViewController的imageView中的TabViewController中显示所选图像。

但没有图像出现。谁能告诉我哪里错了。

这是我所做的代码。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

if (collectionView.tag == 6) {

        [self performSegueWithIdentifier:@"detailController" sender:self];
        _lastSelectedImage = [UIImage imageNamed:_myPatternString];

    }

}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    DetailViewController *detailObject = (DetailViewController *) segue.destinationViewController;

    detailObject.selectedImageInPreviousVC = _lastSelectedImage;

}

和DetailViewController.m

- (void)viewDidLoad {
 [super viewDidLoad];
    _myImageView.image = _selectedImageInPreviousVC;

}

2 个答案:

答案 0 :(得分:1)

您可以直接在performSegueWithIdentifier.

中发送图片

使用类似的东西:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView.tag == 6) {

        [self performSegueWithIdentifier:@"detailController" sender:[UIImage imageNamed:_myPatternString]];
    }
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    DetailViewController *detailObject = (DetailViewController *) segue.destinationViewController;

    detailObject.selectedImageInPreviousVC = sender;
}

另请确保detailObject.selectedImageInPreviousVC不是weak property

答案 1 :(得分:1)

试试这个:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 6) {

// Perform segue need to be called after _lastSelectedImage has been set

        _lastSelectedImage = [UIImage imageNamed:_myPatternString];
        [self performSegueWithIdentifier:@"detailController" sender:self]; 
    }

}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    DetailViewController *detailObject = [segue destinationViewController];
    detailObject.selectedImageInPreviousVC = _lastSelectedImage;
}