Lee Sugden
Lee Sugden

Reputation: 63

UIImage not being received via first view controller

Hi there i currently have just "think i have cracked sending content through a prepare for segue" but the problem is that my images are not showing up in the second view controller. As I'm kind of new to this i would like to know if there is something that i am missing because no matter what i can't seem to get the image of my first view controller to show in my second view controller.

GroupsViewController.h

#import <UIKit/UIKit.h>
@interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate> 
@property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;

- (IBAction)cellToggleAction:(id)sender;

@end

GroupsViewController.m

#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"


@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon"; //set inital value of reuse identifier

    arrayOfImages = [[NSArray alloc]initWithObjects:@"a.png", nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A", nil];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath];
}

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

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
    {
        NSIndexPath* indexPath = [[self.GroupsCollectionView indexPathsForSelectedItems]firstObject];
        if(indexPath !=nil)
        {
            //NSString *selectedDescription = arrayOfDescriptions[indexPath.item]; //collects description from cell
            NSString *selectedImage = arrayOfImages [indexPath.item]; //collects image from cell

            GroupsHomeViewController *groupsHomeViewController = segue.destinationViewController;
            groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImage];
            //groupsHomeViewController.groupLabel = [:selectedDescription];

        }
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
        //Dispose of any resources that can be recreated.
    }

@end

GroupsHomeViewController.h

#import <UIKit/UIKit.h>

@interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIImage *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end

GroupsHomeViewController.m

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

-(void)viewDidLoad{
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end 

Thank You in advance for your kind help.

Upvotes: 0

Views: 65

Answers (3)

Mike Critchley
Mike Critchley

Reputation: 1682

I think the guys above may have solved this with the advice to set the image to whatever image view is holding it. But if that still hasn't solved it, with prepareForSegue, I've had problems due to iOS voodoo where the variable isn't set. So to be sure, override the setter in your GroupsHomeViewController.m to make sure the object is properly assigned to your image property.

- (void)setLogoImage:(UIImage *)logoImage
{
    _logoImage = logoImage;
}

Upvotes: 0

Leejay Schmidt
Leejay Schmidt

Reputation: 1223

The issue lies in your GroupsHomeViewController. You need a UIImageView as your IBOutlet rather than a UIImage, then you need to set the image property on the image view to your desired image. To do this, you need to update your GroupsHomeViewController.h file like so:

@interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *logoImageView;
@property (strong, nonatomic) UIImage *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end

And then in your GroupsHomeViewController.m, set your image view's image in the viewWillAppear:

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

Now, when you set logoImage in your segue, the image view will load the image just before the view appears.

This is assuming that you have the image view already on your storyboard with the outlet correctly connected.

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

Lee Sugden,

Passing an image is not enough :) If you want it to show on your screen you should load it in an ImageView isn't it :)

Now either create UIImageView programmatically or add a imageView in your story board draw an IBOutlet and set the image as

self.yourImageView.image = self.logoImage;

else create an UIImageView programmatically as,

UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.logoImage.size.width,self.logoImage.size.height)];
  yourImageView.image = self.logoImage;
  [self.view addSubview:yourImageView];

Upvotes: 2

Related Questions