2015-02-10 72 views
1

我有一个UICollectionView它有2个部分。每个部分标题都有一个按钮。我试图将部分ID发送到prepareForSegue上的另一个视图控制器,如下所示。如何识别哪个按钮点击哪个部分UICollectionView iOS?

if ([[segue identifier] isEqualToString:@"ManualCapture"]){ 
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender]; 

    ManualCapture *vc = [segue destinationViewController]; 
    vc.sectionId = indexPath.section; 
} 

但它发送0作为部分ID。有没有任何可能的方式来获得部分ID?如何根据按钮点击发送sectionId。基本上我想这样做,当我点击第1部分中的按钮,在prepareForSegue上它应该被发送0,如果我点击了第2节中的按钮,在prepareForSegue上它应该被发送1.我该怎么做?

在此先感谢!在集合视图的委托方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *tagStr = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]; 
yourButton.tag = [tagStr intValue]; 
} 

然后当你要检查哪个部分或指数是获得它通过它的标签,就像这样:

+0

'sender'确实是单元格还是实际上是按钮?它需要是'indexPathForCell:'的单元格才能工作。 – rmaddy 2015-02-10 05:25:51

+0

实际上是一个按钮。那我该怎么做? – codebot 2015-02-10 05:27:58

+0

如果你将一个非正确的单元格传递给'indexPathForCell:',那么得到的'indexPath'将会是'nil',这就是为什么你得到0的部分。 – rmaddy 2015-02-10 05:29:28

回答

3

你可以像下面这样做

UIButton *temp = (UIButton *)sender; 
NSString *tempStr = [NSString stringWithFormat:@"%d",temp.tag]; 

然后比较它。

相关问题