如何将indexPath从页脚内的按钮传递到segue?

时间:2015-04-23 12:44:11

标签: ios objective-c iphone nsindexpath

我正在开发一个自定义布局collectionView,它具有被子外观,每个部分都有一个页脚。两个按钮位于页脚内部,每个按钮都有一个到另一个视图的segue(我使用IB设置segues)。

Footer有自己的indexPath,它是在UICollectionViewFlowLayout类中指定的(我在其中制作了自定义布局)。页脚的indexPath是这样的 - {section - row} {0-0} {1-0} {2-0} ....

我想保留此indexPath信息以在另一个链接到两个按钮的视图中使用。

我试过" convertPoint:toView:... indexPathForRowAtPoint"方式,但没有工作。我认为这是因为按钮不属于collectionViewItem但属于Footer。

在这种情况下,将每个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?

    }
}

以下是Footer类的标题

#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

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

想象一个带有两个按钮的可重用视图(leftButton和rightButton)。在可重用的视图类.h文件中为索引路径添加属性:

@property (strong, nonatomic) NSIndexPath *indexPath;

然后在你的viewcontrollers 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];
}

最后在你的viewcontrollers 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

的工作。当然,你必须在故事板中连接segues LeftButtonSegue RightButtonSegue