如何将 indexPath 从页脚内的按钮传递到 segue?
How to pass indexPath from button inside footer to segue?
我正在开发一个自定义布局 collectionView,它具有被子外观并且每个部分都有一个页脚。页脚内有两个按钮,每个按钮都有一个到另一个视图的转场(我使用 IB 设置转场)。
Footer 有自己的 indexPath,它是在 UICollectionViewFlowLayout class(我在这里制作了自定义布局)中分配的。 footer的indexPath是这样的- {section - row} {0-0} {1-0} {2-0}....
我想保留此 indexPath 信息以在链接到两个按钮的另一个视图中使用。
我尝试了 "convertPoint: toView: ... indexPathForRowAtPoint" 方法,但没有用。我认为这是因为按钮不属于 collectionViewItem 而属于页脚。
在这种情况下,将每个 footerView 的 indexPath 传递给 segue 的最佳方式是什么?
任何带有代码示例的解决方案将不胜感激。
// CollectionView DataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
Footer *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:@"Footer"
forIndexPath:indexPath];
// set button images and labels
[footerview setButton];
// debug code : show current section number
footerview.footerLabel.text = [NSString stringWithFormat:@"Section %li", (long)indexPath.section];
return footerview;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"SeeAllPictures"]) {
// how to pass indextPath?
}
else if([[segue identifier] isEqualToString:@"GoDiary"]) {
// how to pass indextPath?
}
}
下面是页脚 header class
#import <UIKit/UIKit.h>
@interface Footer : UICollectionReusableView
@property (strong, nonatomic) IBOutlet UILabel *footerLabel;
@property (strong, nonatomic) IBOutlet UIButton *seeAllPictures;
@property (strong, nonatomic) IBOutlet UIButton *goDiary;
@property (strong, nonatomic) IBOutlet UIImageView *seeAllPicturesImage;
@property (strong, nonatomic) IBOutlet UILabel *seeAllPicturesLabel;
@property (strong, nonatomic) IBOutlet UIImageView *goDiaryImage;
@property (strong, nonatomic) IBOutlet UILabel *goDiaryLabel;
- (void)setButton;
@end
尝试以下操作:
想象一个带有两个按钮(左按钮和右按钮)的可重用视图。在您的可重用视图 类 .h 文件中为索引路径添加一个 属性:
@property (strong, nonatomic) NSIndexPath *indexPath;
然后在您的视图控制器中 viewForSupplementaryElementOfKind 方法:
CustomFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerView.indexPath = indexPath;
[footerView.leftButton addTarget:self action:@selector(leftButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[footerView.rightButton addTarget:self action:@selector(rightButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
然后实现两个方法:
- (void)leftButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"LeftButtonSegue" sender:sender];
}
- (void)rightButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"RightButtonSegue" sender:sender];
}
最后在您的视图控制器中 prepareForSegue 方法:
CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview;
NSLog(@"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
重要
按钮必须是可重用视图的直接子元素才能制作
(CustomFooterView *)((UIButton *)sender).superview
工作。当然,你必须在情节提要中连接 LeftButtonSegue 和 RightButtonSegue
我正在开发一个自定义布局 collectionView,它具有被子外观并且每个部分都有一个页脚。页脚内有两个按钮,每个按钮都有一个到另一个视图的转场(我使用 IB 设置转场)。
Footer 有自己的 indexPath,它是在 UICollectionViewFlowLayout class(我在这里制作了自定义布局)中分配的。 footer的indexPath是这样的- {section - row} {0-0} {1-0} {2-0}....
我想保留此 indexPath 信息以在链接到两个按钮的另一个视图中使用。
我尝试了 "convertPoint: toView: ... indexPathForRowAtPoint" 方法,但没有用。我认为这是因为按钮不属于 collectionViewItem 而属于页脚。
在这种情况下,将每个 footerView 的 indexPath 传递给 segue 的最佳方式是什么?
任何带有代码示例的解决方案将不胜感激。
// CollectionView DataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
Footer *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:@"Footer"
forIndexPath:indexPath];
// set button images and labels
[footerview setButton];
// debug code : show current section number
footerview.footerLabel.text = [NSString stringWithFormat:@"Section %li", (long)indexPath.section];
return footerview;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"SeeAllPictures"]) {
// how to pass indextPath?
}
else if([[segue identifier] isEqualToString:@"GoDiary"]) {
// how to pass indextPath?
}
}
下面是页脚 header class
#import <UIKit/UIKit.h>
@interface Footer : UICollectionReusableView
@property (strong, nonatomic) IBOutlet UILabel *footerLabel;
@property (strong, nonatomic) IBOutlet UIButton *seeAllPictures;
@property (strong, nonatomic) IBOutlet UIButton *goDiary;
@property (strong, nonatomic) IBOutlet UIImageView *seeAllPicturesImage;
@property (strong, nonatomic) IBOutlet UILabel *seeAllPicturesLabel;
@property (strong, nonatomic) IBOutlet UIImageView *goDiaryImage;
@property (strong, nonatomic) IBOutlet UILabel *goDiaryLabel;
- (void)setButton;
@end
尝试以下操作:
想象一个带有两个按钮(左按钮和右按钮)的可重用视图。在您的可重用视图 类 .h 文件中为索引路径添加一个 属性:
@property (strong, nonatomic) NSIndexPath *indexPath;
然后在您的视图控制器中 viewForSupplementaryElementOfKind 方法:
CustomFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
footerView.indexPath = indexPath;
[footerView.leftButton addTarget:self action:@selector(leftButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[footerView.rightButton addTarget:self action:@selector(rightButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
然后实现两个方法:
- (void)leftButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"LeftButtonSegue" sender:sender];
}
- (void)rightButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:@"RightButtonSegue" sender:sender];
}
最后在您的视图控制器中 prepareForSegue 方法:
CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview;
NSLog(@"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
重要
按钮必须是可重用视图的直接子元素才能制作
(CustomFooterView *)((UIButton *)sender).superview
工作。当然,你必须在情节提要中连接 LeftButtonSegue 和 RightButtonSegue