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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.