I have looked all over this website for 3 hours now and i cannot find out how to do this without referencing to manually connecting all of my 106 collection view cells with segues to another ViewController.
I would like to be able to click on my CustomCell (we will call it A) when i click on A i want to open a new viewController with the Page Title being A and the image i have set in the CustomCell to be the Page Logo within a UIImage on the view controller page.
I am aware this has to do with passing information between ViewControllers and i have no idea how to do this. Therefore i am very stuck.
I have only been using Xcode for a month now and i need it for my apprenticeship project so it would be very helpful if someone could help me.
My Current code is as follows, if someone is so kind enough to help please could you comment your code so that i get a further understanding as i don't just want to copy and paste the code and learn nothing from it.
Many thanks in advance.
ViewController.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";
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:@"customSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = (NSIndexPath *)sender;
UIImage *imageToShow = [UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]];
NSString *titleToShow = [arrayOfDescriptions objectAtIndex:indexPath.item];
GroupsHomeViewController * destination = (GroupsHomeViewController *)segue.destinationViewController;
destination.groupLabel = *titleToShow; //assigning to UILabel form incompatible type NSString
destination.logoImage = *imageToShow; // UIImageView from incompatible type UImage
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
// Toggle View Button
- (IBAction)cellToggleAction:(id)sender {
if([reuseIdentifier isEqualToString:@"SmallIcon"]){
reuseIdentifier=@"ListView";
[sender setImage:[UIImage imageNamed:@"LargeIcon"]];
}
else if
([reuseIdentifier isEqualToString:@"ListView"]){
reuseIdentifier=@"LargeIcon";
[sender setImage:[UIImage imageNamed:@"SmallIcon"]];
}
else if
([reuseIdentifier isEqualToString:@"LargeIcon"]){
reuseIdentifier=@"SmallIcon";
[sender setImage:[UIImage imageNamed:@"ListView"]];
}
[self.GroupsCollectionView reloadData];
}
//Toggled Cell Sizes
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize;
if([reuseIdentifier isEqualToString:@"SmallIcon"])
cellSize = CGSizeMake(100, 130);
else if
([reuseIdentifier isEqualToString:@"ListView"])
cellSize = CGSizeMake(320, 50);
else if
([reuseIdentifier isEqualToString:@"LargeIcon"])
cellSize = CGSizeMake(320, 350);
return cellSize;
}
@end
GroupsHomeViewController.m where the groups view controller pay goes to.
#import "GroupsHomeViewController.h"
@interface GroupsHomeViewController ()
@end
@implementation GroupsHomeViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.groupLabel.text = self.groupLabel; //incompatible pointertypes assigning to NSSstring
self.logoImage.image = self.logoImage; //incompatible pointer types assigning to NSSstring
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
CustomeCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *IconImage;
@property (weak, nonatomic) IBOutlet UILabel *IconLabel;
@end