1

我做了以下事情:

 buttonPlaylistView = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width *(urlList.count+1), 0, self.view.frame.size.width, self.view.frame.size.height)];
            buttonPlaylistView.tag = 0;

 UITapGestureRecognizer *doubleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
            [doubleTap3 setNumberOfTapsRequired:2];
            [buttonPlaylistView addGestureRecognizer:doubleTap3];
            [doubleTap3 release];

-(void) handleDoubleTap:(UITapGestureRecognizer *)sender{
    if(sender.state == UIGestureRecognizerStateEnded)

    int x = [sender tag];
    return;
}

但我明白SIGAGRT了这一点:int x = [sender tag];说:

[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x61280b0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]: unrecognized selector sent to instance 0x61280b0'

现在:这是什么问题以及解决方案是什么?谢谢

4

4 回答 4

12
-(void) handleDoubleTap:(UITapGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateEnded)
    {
      int x = [sender.view tag];
    }
}

将解决问题。

于 2011-11-08T13:32:43.573 回答
1

UITapGestureRecognizer没有名为 tag 的属性 - 如您所见,您获得的发件人不是按钮。您必须直接访问buttonPlayListView,例如

int x = [buttonPlayListView tag];

或以其他方式记住您要访问的按钮。

于 2011-11-08T13:24:57.630 回答
1

即使我很确定您以错误的方式进行此操作,向 UIButton 添加双击手势识别器,但仍有一种方法可以执行您需要的任务,而这对您来说不应该是太多的工作. 你发表了评论

如果我创建了 100 个按钮,我怎么记得

对于其中一个答案,该答案突出了导致您的 SIGBART 的问题。UIGestureRecognizer 没有标签属性。

这是您可以做的,是遍历 [self view] 的所有子视图并找到具有相同 UIGestureRecognizer 的子视图,这不是最漂亮的解决方案,并且您拥有的子视图越多,循环所需的时间就越长。但它会做你似乎正在寻找的东西,所以如果你添加 .

在您的 handleDoubleTap 函数中,您可以执行以下操作

-(void) handleDoubleTap:(UITapGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateEnded)
    {
        int iButtonTag = -1 //This is important later to escape the below for loop as we don't need to needlessly go around in circles
        for(UIView* psubView in [[self view] subviews])
        {
            if( [psubView isKindOfClass:[UIButton class]] )
            {
                UIButton* pButton = (UIButton*)psubView;
                for(UIGestureRecognizer* pGesture in [pButton gestureRecognizers] )
                {
                     if( pGesture == sender )//this is the button we're after
                     {
                         iButtonTag = [pButton tag];
                         break;
                     }
                }
                if( iButton != -1 )//found what we came for
                {
                    break;
                }
             }
        }
        //do what ever it was you needed to do now that you have the views tag, or you could have kept a reference to the button etc.
    }
 }

那应该可以解决您的问题。或者,如果您要向子视图的子视图添加按钮,最好在 NSMutableArray 中跟踪您的 UIButtons ,您可以通过创建类属性(或成员变量)并使用'addObject:' NSMutableArray 的函数。然后代替线

for(UIView* psubView in [[self view] subviews])

上面你可以换成

for( UIButton* pButton in m_pMutableButtonArray )

其中“m_pMutableButtonArray”是您为存储 UIButtons 的 NSMutableArray 提供的变量名称。这也意味着如果在下一行进行 isKindOfClass 测试,您将取消以下操作。

那应该可以解决您的问题。

于 2011-11-08T14:19:28.280 回答
0

为什么要将 UITapGestureRecognizer 放在按钮中?该按钮已经为您处理了,并将向您发送回调,您可以使用此 UIControl 方法将目标添加到按钮

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
于 2011-11-08T13:31:45.650 回答