【问题标题】:How to pass indexPath from button inside footer to segue?如何将 indexPath 从页脚内的按钮传递到 segue?
【发布时间】:2015-04-23 12:44:11
【问题描述】:

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

页脚有自己的 indexPath,它在 UICollectionViewFlowLayout 类中分配(我在其中制作了自定义布局)。 footer 的 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

【问题讨论】:

    标签: ios objective-c iphone nsindexpath


    【解决方案1】:

    尝试以下方法:

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

    @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
    

    工作。当然,您必须在情节提要中连接 LeftButtonSegueRightButtonSegue

    【讨论】:

    • 这行得通!万分感谢!!英语不是我的第一语言,所以你的代码示例真的帮助我理解了。再次感谢!
    • 不客气 :) 也许您可以投票给我的答案并将其设置为接受的答案?!很高兴我能帮上忙!
    • 说起来很尴尬,但我在你的解决方案之后坚持了下来。你能看看我的下一个问题吗? link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多