首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS为什么不能从UITapGestureRecognizer中调用方式UITapGestureRecognizer:didSelectItemAtIndexPath: be?

iOS为什么不能从UITapGestureRecognizer中调用方式UITapGestureRecognizer:didSelectItemAtIndexPath: be?
EN

Stack Overflow用户
提问于 2014-01-23 06:12:48
回答 3查看 957关注 0票数 0

我在UITapGestureRecognizer中为UIScrollView设置了一个UICollectionView。我已经将它配置为正确地检测点击并触发我编写的方法,但是如果我试图将选择器设置为collectionView:didSelectItemAtIndexPath:程序在单元格被点击时崩溃。

知道为什么会这样吗?

这工作:

代码语言:javascript
复制
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];

- (void) tapped:(UIGestureRecognizer *)gesture{
//some code
}

--这不起作用:

代码语言:javascript
复制
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//some code
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-23 06:26:07

你写的代码,

代码语言:javascript
复制
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];

选择器通常只是一个singleFunction,其中有一个输入参数,即UITapGestureRecogniser对象。

应该是这样,

代码语言:javascript
复制
-(void)clicked:(UIGestureRecogniser *)ges{

}

但是您使用的选择器不正确,因为它需要两个不能与gestureRecogniser.Hence一起提供的输入--崩溃。

将上述代码更改为1以下,

代码语言:javascript
复制
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];

}
票数 5
EN

Stack Overflow用户

发布于 2014-01-23 06:28:44

手势识别器的操作必须符合下列签名之一:

代码语言:javascript
复制
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

您需要使用这些动作签名之一,并在该方法中执行所需的任何操作,包括为手势确定正确的indexPath

见docs:ref/occ/instm/UIGestureRecognizer/initWithTarget:action

票数 1
EN

Stack Overflow用户

发布于 2014-01-23 06:29:40

我们必须从正确的引用对象调用didSelectItemAtIndexPath。

代码语言:javascript
复制
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];

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21300732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档