Ajout d'une UICollectionView au storyboard : pourquoi ça ne marche pas ?
re-Bonjour,
voici une question plus précise, relative à l'architecture des UIViewController.
dans mon AppDelegate, je fais :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController * mainViewController = [[UIStoryboard storyboardWithName:@Main bundle:nil] instantiateViewControllerWithIdentifier:@mainVC];
CBDMenuCollectionVC * menuVC = [[CBDMenuCollectionVC alloc] init] ;
[mainViewController addChildViewController:menuVC] ;
[mainViewController.view addSubview:menuVC.view] ;
return YES;
}
Voilà ma sous-classe de UICollectioView
//
// CBDMenuCollectionVC.m
// TestNavigation
//
// Created by Colas on 02/06/2014.
// Copyright (c) 2014 Colas. All rights reserved.
//
#import "CBDMenuCollectionVC.h"
#import "CBDMenuButtonCell.h"
@interface CBDMenuCollectionVC ()
@property (nonatomic, readonly, strong) NSMutableArray * buttons ;
@end
@implementation CBDMenuCollectionVC
- (id)init
{
self = [super initWithNibName:@CBDMenuCollectionVC
bundle:nil];
if (self)
{
_buttons = [[NSMutableArray alloc] init] ;
}
return self ;
}
- (void)createButtons
{
UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(log forControlEvents:UIControlEventTouchUpInside];
[button1 setBackgroundImage:[UIImage imageNamed:@button1.png] forState:UIControlStateNormal];
button1.frame = CGRectMake(14.0, 10.0, 74.0, 46.0);
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(log forControlEvents:UIControlEventTouchUpInside];
[button1 setBackgroundImage:[UIImage imageNamed:@button2.png] forState:UIControlStateNormal];
button1.frame = CGRectMake(14.0, 10.0, 74.0, 46.0);
UIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(log forControlEvents:UIControlEventTouchUpInside];
[button1 setBackgroundImage:[UIImage imageNamed:@button3.png] forState:UIControlStateNormal];
button1.frame = CGRectMake(14.0, 10.0, 74.0, 46.0);
UIButton * button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(log forControlEvents:UIControlEventTouchUpInside];
[button1 setBackgroundImage:[UIImage imageNamed:@button4.png] forState:UIControlStateNormal];
button1.frame = CGRectMake(14.0, 10.0, 74.0, 46.0);
[self.buttons addObject:button1] ;
[self.buttons addObject:button2] ;
[self.buttons addObject:button3] ;
[self.buttons addObject:button4] ;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
//
/**************************************/
#pragma mark - Datasource methods
/**************************************/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @buttonCell;
CBDMenuButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier
forIndexPath:indexPath];
[cell.view addSubview:self.buttons[indexPath.row]] ;
return cell ;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1 ;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [self.buttons count] ;
}
//
//
/**************************************/
#pragma mark - Delegate methods
/**************************************/
- (void)log:(id)sender
{
NSLog(@Hello) ;
}
Quant à CBDMenuButtonCell, c'est une classe à peu près vide pour l'instant. J'ai juste ajouté un UIView dans le .xib et créé un IBOutlet. J'ai aussi ajouté le identifier.
Pourquoi rien ne s'affiche ? J'imagine que je fais une erreur grossière ou que je passe à côté de quelque chose d'évident.
Merci si vous voyez quoi !
Je mets mon projet-test en pièce jointe.
Merci !
Colas
PS : j'ai bien testé que menuVC.view n'était pas nil (le xib est bien chargé), et le datasource est bien linké.
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
J'ai regardé en diagonale...
Il ne te manquerait pas un "rootViewController" ?
Edit : En ajoutant ceci à la fin de ton AppDelegate :
Merci @Alf, ça marche.
De toute façon, maintenant, je n'utilise plus les storyboards.
Je pensais qu'avec les Storyboards (et l'info Main Interface), le code que tu m'as donné n'était pas nécessaire.
Merci de ton aide !