amurcia amurcia - 1 year ago 48
iOS Question

UICollectionView - Add action to footer view

I have a UICollectionView with a footer view. Footer view class is created by me. Footer view is a button that I want to put an action. I tried to add one action but nothing happened when I pressed the button. Please help me.

My code:

FViewController.m

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionFooter) {

FooterCollectionReusableView *footerview = [[FooterCollectionReusableView alloc] init];
footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

[footerview.todoButton addTarget:self action:@selector(todosPush:) forControlEvents:UIControlEventTouchUpInside];


reusableview = footerview;
}

return reusableview;
}

-(IBAction)todosPush:(id)sender{

NSLog(@"todosPush");
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TipoEjercicioViewController *tipo = [storyBoard instantiateViewControllerWithIdentifier:@"TipoEjercicioView"];
tipo.ejercicio = 22;
[self.navigationController pushViewController:tipo animated:YES];
}


FooterCollectionReusableView.h

@interface FooterCollectionReusableView : UICollectionReusableView

@property (nonatomic, retain) IBOutlet UIButton * todoButton;

@end

Answer Source

Try this:

Add following lines

FooterCollectionReusableView *footerview = (FooterCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

and

return (UICollectionReusableView *)footerview;

Also check is your todoButton is connected to Button in storyboard or not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download