简体   繁体   中英

Selected image is not coming in Detail View Controller

I have a CollectionView in a viewcontroller. I named this viewController as TabViewController. Now in that collectionView I have some images. Now when I am tapping on the cells it is pushing to another viewController named as DetailViewController. In DetailViewController I have an imageView. I want to show the selected image in TabViewController in the imageView of DetailViewController.

But no images are coming. Can any one show me where I am wrong.

This is the code that I have done.

-(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;

}

and in DetailViewController.m

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

}

You could directly sent the image in performSegueWithIdentifier.

Use something like this:

-(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;
}

Also make sure that detailObject.selectedImageInPreviousVC is not a weak property .

Try this:

-(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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM