我有一个启用了页脚的UICollectionView
。我创建了一个名为UICollectionReusableView
的LeafletFooterView
的自定义子类,并声明了IBOutlet
和IBAction
:
- (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。该方法绝对称为; NSLog
和Breakpoint
证明了这一点。更新
因此,在该操作不起作用之后,我尝试按照与此问题相同的方式来完成声明委托的过程: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
。在viewWillAppear
的LeafletCollectionView
中,我尝试了以下操作: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 *)
,我希望它能起作用:)