Lee Sugden Lee Sugden - 6 months ago 21
Objective-C Question

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.

Answer

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