0

I have the same code in three pages, but in two of them, this error occurs:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialised with a non-nil layout parameter

I don't understand why, if the code is the same in three pages and in the first page i don't have any problem.

My Code in header file

#import <UIKit/UIKit.h>

@interface CollectionViewController : UICollectionViewController<UICollectionViewDataSource>

@property (nonatomic,strong) NSMutableArray *marrImages;
@property (nonatomic,strong) NSMutableDictionary *mdictImageData;



@end

and in .m file

@interface CollectionViewController ()

@end

@implementation CollectionViewController


@synthesize marrImages,mdictImageData;

static NSString * const reuseIdentifier = @"cell";


- (void)viewDidLoad {
    [super viewDidLoad];

    //navigation bar
    UIImage *image = [UIImage imageNamed:@"home.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 50, 50);
    [backButton setImage:image forState:UIControlStateNormal];
    //    [backButton addTarget:self action:@selector(leftButtonAcion:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = button2;
    self.title = @"Welcome";

    marrImages=[[NSMutableArray alloc]init];

    mdictImageData=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"circlshadow_parking.png",@"imageFile",@"Estacionamentos",@"Info",nil];
    [marrImages addObject:mdictImageData];

   mdictImageData=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"circlshadow_charging.png",@"imageFile",@"Carregar Saldo",@"Info", nil];
    [marrImages addObject:mdictImageData];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>

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


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.lblIndex.text=[[marrImages objectAtIndex:[indexPath row]]valueForKey:@"Info"];
    UIImage *image=[UIImage imageNamed:[[marrImages objectAtIndex:[indexPath row]]valueForKey:@"imageFile"]];
    [cell.ivCartoon setImage:image];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];




    if(lastIndex == 0)
    {
        ParkingMenu *parkingMenu = [[ParkingMenu alloc] init];
        parkingMenu.modalPresentationStyle = UIModalPresentationPageSheet;
        parkingMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.navigationController pushViewController:parkingMenu animated:YES];
        [parkingMenu release];

    }
    else if(lastIndex == 1)
    {

        BalanceMenu *balanceMenu = [[BalanceMenu alloc] init];
        balanceMenu.modalPresentationStyle = UIModalPresentationPageSheet;
        balanceMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.navigationController pushViewController:balanceMenu animated:YES];
        [balanceMenu release];

    }
    else if(lastIndex == 2)
    {
    }
    else if(lastIndex == 3)
    {
    }
}

@end
Spidy
  • 1,190
  • 3
  • 29
  • 47
HideCode
  • 173
  • 2
  • 9
  • How are these controller instances created (show code)? – Wain Dec 07 '15 at 16:15
  • I have my views in a storyboard. This is my first app , for which I am not much in the know. I need to instantiate somewhere is that with the former did not do it and everything is fine. – HideCode Dec 07 '15 at 16:25
  • So it;s the parking and balance 'pages' that have a problem? And they are defined in the storyboard? How are they defined in the storyboard? – Wain Dec 07 '15 at 16:40
  • My storyboard: http://postimg.org/image/50natv1st/ – HideCode Dec 07 '15 at 16:45
  • and now? http://s30.postimg.org/srmobz201/Captura_de_Tela_2015_12_07_s_16_18_05.png – HideCode Dec 07 '15 at 16:57

1 Answers1

0

This code:

    ParkingMenu *parkingMenu = [[ParkingMenu alloc] init];
    parkingMenu.modalPresentationStyle = UIModalPresentationPageSheet;
    parkingMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController pushViewController:parkingMenu animated:YES];
    [parkingMenu release];

shows that you're creating an instance of ParkingMenu without referencing the storyboard. Presumably this is a subclass of UICollectionViewController and is resulting in you not using the designated initialiser initWithCollectionViewLayout: or unarchiving from the storyboard (which would also set the layout).

You're also not using ARC, so you have to call release, which you shouldn't do really...

You need to update your code so it triggers a segue between the view controllers or so it loads the ParkingMenu from the storyboard by identifier.

Wain
  • 117,132
  • 14
  • 131
  • 151
  • That depends on what's in the storyboard. Can't see your image as it's blocked here... – Wain Dec 07 '15 at 17:34
  • I resolve the problem with this UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"ParkingMenu"]; [self presentViewController:vc animated:YES completion:NULL]; but the navigation bar dissapears on the two next pages – HideCode Dec 07 '15 at 17:40
  • this is the by identifier approach (as opposed to a segue) – Wain Dec 07 '15 at 17:55
  • I did not understand what you mean. sorry – HideCode Dec 07 '15 at 18:24
  • In my answer 1 listed 2 potential solutions, that code is the second of them, just for your information – Wain Dec 07 '15 at 19:19