我有一个启用了页脚的UICollectionView。我创建了一个名为UICollectionReusableViewLeafletFooterView的自定义子类,并声明了IBOutletIBAction:

- (IBAction)dropboxButton:(id)sender;
- (IBAction)soundcloudButton:(id)sender;

在我的UICollectionViewClass中,我有:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];

        reusableview = footerview;
    }

    return reusableview;
}

IBAction类的LeafletFooterView中,我只是尝试呈现另一个viewController:
- (IBAction)dropboxButton:(id)sender
{
    VideoWebViewController *video = [[VideoWebViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:video];

    [self presentViewController:navigation animated:YES completion:nil];

    [video setSelectedVideo:@"https://www.dropbox.com/"];
}

错误是:
No visible @interface for 'LeafletFooterView' declares the selector 'presentViewController:animated:completion'.

我已经在[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.dropbox.com"]];方法中尝试了IBAction,尽管现在没有错误,但是单击它不会在网站上打开Safari。该方法绝对称为; NSLogBreakpoint证明了这一点。

更新

因此,在该操作不起作用之后,我尝试按照与此问题相同的方式来完成声明委托的过程:Present view controller from NSObject subclass

这是我的LeafletFooterView:
@class LeafletFooterView;
@protocol FooterViewDelegate <NSObject>
-(void)rowTapped;
@end
@interface LeafletFooterView : UICollectionReusableView
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)dropboxButton:(id)sender;
@property(nonatomic,assign)id<FooterViewDelegate> delegate;

The LeafletFooterView.m is here: - (IBAction)dropboxButton:(id)sender
{
    NSLog(@"Dropbox");
    [self.delegate rowTapped];
}

现在在我的LeafletCollectionViewController中,我有:
- (void)rowTapped
{
    NSLog(@"Is this getting called?");
    VideoWebViewController *video = [[VideoWebViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:video];

    [self presentViewController:navigation animated:YES completion:nil];

    [video setSelectedVideo:@"https://www.dropbox.com/"];

}

当我运行此代码时,实际上不会调用此方法。 LeafletCollectionView设置为使用FooterViewDelegate。在viewWillAppearLeafletCollectionView中,我尝试了以下操作:
LeafletFooterView *footer = [[LeafletFooterView alloc] init];
footer.delegate = self;
rowTapped方法仍然从未真正被调用。

用户应该能够单击UIButton中的LeafletFooterView并显示UIViewController。我该如何实现这一目标?任何指导将不胜感激。

最佳答案

您需要提供UINavigationController所在的dataSource。因此,如果您尝试删除该IBAction,而是以编程方式将方法添加到dataSource将要呈现VideoWebViewController的任何位置。

然后,在viewForSupplementaryElementOfKind方法中,您将访问该按钮并触发该方法。所以离开我的头顶(有一段时间没有使用objC了):

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

    if (kind == UICollectionElementKindSectionFooter) {
        LeafletFooterView *footerview = (LeafletFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        [footerView.dropboxButton addTarget:self
                       action:@selector(dropboxTapped:)
             forControlEvents:UIControlEventTouchUpInside];

        reusableview = footerview;
    }

    return reusableview;
}

-(void)dropboxTapped:(id)sender{
    // Present the controller here
}

我希望这有帮助 :)

编辑:更新您的问题后,我看到您走了一条不同的路线。您只需要为footerView设置委托,如下所示:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionFooter) {
        LeafletFooterView *footerview = (LeafletFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        [footerView.dropboxButton addTarget:self
                                     action:@selector(dropboxTapped:)
                           forControlEvents:UIControlEventTouchUpInside];
        footerView.delegate = self
        reusableview = footerview;
    }

    return reusableview;
}

另外,不要忘记像使用LeafletFooterView在代码中一样将视图转换为(LeafletFooterView *),我希望它能起作用:)

09-16 05:56