我在UITapGestureRecognizer中为UIScrollView设置了一个UICollectionView。我已经将它配置为正确地检测点击并触发我编写的方法,但是如果我试图将选择器设置为collectionView:didSelectItemAtIndexPath:程序在单元格被点击时崩溃。
知道为什么会这样吗?
这工作:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- (void) tapped:(UIGestureRecognizer *)gesture{
//some code
}--这不起作用:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//some code
}发布于 2014-01-23 06:26:07
你写的代码,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];选择器通常只是一个singleFunction,其中有一个输入参数,即UITapGestureRecogniser对象。
应该是这样,
-(void)clicked:(UIGestureRecogniser *)ges{
}但是您使用的选择器不正确,因为它需要两个不能与gestureRecogniser.Hence一起提供的输入--崩溃。
将上述代码更改为1以下,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicked:)];
-(void)clicked:(UIgestureRecogniser *)ges{
//use gesture to get get the indexPath, using CGPoint (locationInView).
NSIndexPath *indexPath = ...;
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
}发布于 2014-01-23 06:28:44
手势识别器的操作必须符合下列签名之一:
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;您需要使用这些动作签名之一,并在该方法中执行所需的任何操作,包括为手势确定正确的indexPath。
见docs:ref/occ/instm/UIGestureRecognizer/initWithTarget:action
发布于 2014-01-23 06:29:40
我们必须从正确的引用对象调用didSelectItemAtIndexPath。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- (void) tapped:(UIGestureRecognizer *)gesture{
NSIndexPath *indexPath = //create your custom index path here
[self.collectionViewObject didSelectItemAtIndexPath:indexPath];
}https://stackoverflow.com/questions/21300732
复制相似问题