UICollectionView - 向页脚视图添加操作

UICollectionView - Add action to footer view

我有一个带有页脚视图的 UICollectionView。页脚视图 class 是我创建的。页脚视图是我想要执行操作的按钮。我试图添加一个动作,但当我按下按钮时没有任何反应。请帮助我。

我的代码:

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

试试这个:

添加以下行

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

return (UICollectionReusableView *)footerview;

还要检查你的 todoButton 是否连接到故事板中的 Button。

事情是这样的:

is you connected todoButton with button in storyboard?

我忘记了连接按钮 :( 现在可以使用了

谢谢 Lalit Kumar

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    CGSize footersize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 20);
    return footersize;
}