本文介绍了如何从页脚内的按钮传递indexPath到segue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个自定义布局collectionView,该视图具有被子外观,每个部分都有页脚.页脚内部有两个按钮,每个按钮都有到另一个视图的序列(我使用IB设置序列).

I'm working on a custom layout collectionView which has quilt look and each section has a footer. Two buttons are inside footer and each button has a segue to another view(I set segues using IB).

页脚具有自己的indexPath,该索引路径已在UICollectionViewFlowLayout类中分配(我在此处进行了自定义布局).页脚的indexPath就是这样-{section-row} {0-0} {1-0} {2-0} ....

Footer has its own indexPath which was assigned in a UICollectionViewFlowLayout class(Where I made a custom layout). The indexPath of footer is like this - {section - row} {0-0} {1-0} {2-0}....

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

I want to keep this indexPath information to use at the another view which is linked to two buttons.

我尝试了"convertPoint:toView:... indexPathForRowAtPoint"的方法,但是没有用.我认为这是因为按钮不属于collectionViewItem,而是属于Footer.

I tried "convertPoint: toView: ... indexPathForRowAtPoint" way but didn't work. I think it's because the buttons are not belong to collectionViewItem but belong to Footer.

在这种情况下,将每个footerView的indexPath传递给segue的最佳方法是什么?

In this situation, what is the best way of passing each footerView's indexPath to segue?

任何带有代码示例的解决方案将不胜感激.

Any solution with code example will be greatly appreciated.

// 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类的标题

Below is header of Footer 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

推荐答案

尝试以下操作:

想象一个具有两个按钮(leftButton和rightButton)的可重用视图.在可重用的视图类.h文件中,为indexpath添加一个属性:

imagine a reusable view with two buttons (leftButton and rightButton). in your reusable view classes .h file add a property for the indexpath:

@property (strong, nonatomic) NSIndexPath *indexPath;

然后在您的视图控制器中 viewForSupplementaryElementOfKind 方法:

then in your viewcontrollers viewForSupplementaryElementOfKind method:

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 方法:

and finally in your viewcontrollers prepareForSegue method:

CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview;
NSLog(@"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row);

重要

按钮必须是可重用视图的直接子元素

the buttons have to be direct childs of the reusable view to make

(CustomFooterView *)((UIButton *)sender).superview

工作.当然,您必须在情节提要中连接 LeftButtonSegue RightButtonSegue .

work. and of course you have to connect the segues LeftButtonSegue and RightButtonSegue in storyboard.

这篇关于如何从页脚内的按钮传递indexPath到segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:33