I am working on an iOS app.I have a collection view which have some images.Images come from an array I have created.When I clicked on a cell of collection view, a second view controller opens.I want to show that particular image on second view controller on which I have clicked on the collection view. How to do that?
-
create the string or image in second VC and add the value to this value in first VC, r u using storyboard or xib – Anbu.Karthik May 14 '15 at 11:31
-
possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – luk2302 May 14 '15 at 12:05
3 Answers
Create image object in SecondViewController like below:
@property (nonatomic, strong) UIImageView *detailImageView;
@property (nonatomic, strong) UIImage *detailImage;
In your FirstViewController.m do the same as below:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionViewObject dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.detailImage = cell.imageView.image;
[self.navigationController pushViewController:secondViewController animated:YES];
}
And then in SecondViewController.m viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailImageView.image = self.detailImage;
}
Hope this will work for you!!

- 1,539
- 3
- 17
- 27
I hope you have created UIImageView
in next ViewController
Step 1. Firs of all in you next ViewController.h create image
@property (nonatomic, strong) UIImage *image;
then in previous ViewControler.m find you didSelectItemAtIndexPath
method, or create them, and add next code:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentificer forIndexPath:indexPath];
MyViewController *myvc = [[MyViewController alloc] init];
myvc.image = cell.imageView.image;
[self presentViewController: myvc animated:YES completion:nil];
}
if you don`t using storyboard go to step 3.
Step 2. If you using storyboard create image in you next ViewController.h
then in previous ViewControler.m find you didSelectItemAtIndexPath
method, or create them, and add next code:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"nextVCSeagueName" sender:self];
}
add method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"nextVCSeagueName"]) {
MyviewController *myvc = segue.destinationViewController;
myvc.image = [imagesArray objectAtIndex:[[self.collectionView indexPathsForSelectedItems] firstObject]];
}
}
Step 3. In next ViewController.m in viewDidLoad
add
-(void)viewDidLoad {
[super viewDidLoad];
myImageView.image = self.image;
}

- 530
- 7
- 22
-
@SnigdharaniSahu i'm glad to hear that i can help you, please accept my answer or vote up. – Roman Simenok May 18 '15 at 10:59
Use the delegate method to know which image to pass-
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Let's assume your array of images is called imageArray. In your class, declare an image String like-
@propert NSString *selectedImage;
Now, when you implement the didSelectItemAtIndexPath method, you can save the image that is loaded in that particular row in your selectedImage variable. Then you can implement the prepareForSegue method to pass this variable to your Second View Controller.
Let's assume, you have declared a variable called imageFromFirstviewController in your SecondViewcontroller.h file.
@propert NSString *imageFromFirstviewController;
Now, you can do something like following in your didSelectItemAtIndexPath method-
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.selectedImage = [self.imageArray objectAtIndex:indexPath.row] ;
}
after that pass this image name to the Second View Controller through the segue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// "my-segue-name" which is assign in the storyboard while attaching the segue
if ([[segue identifier] isEqualToString:@"my-segue-name"])
{
SecondViewcontroller *secondVC = [segue destinationViewController];
secondVC.imageFromFirstviewController = self.selectedImage;
}
}
Now, as you have access to your selected image in your second view controller, you can use this imageFromFirstviewController anyway you want.

- 6,651
- 3
- 36
- 58