用于按钮的UITapGestureRecognizer问题

时间:2011-11-08 13:20:40

标签: iphone uibutton uitapgesturerecognizer

我做了以下事情:

 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;
}

但我在这一行得到SIGAGRTint 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 个答案:

答案 0 :(得分:12)

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

将解决问题。

答案 1 :(得分:1)

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

int x = [buttonPlayListView tag];

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

答案 2 :(得分:1)

尽管我很确定你是以错误的方式解决这个问题,但是在UIButton中添加一个双击手势识别器,有一种方法你仍然可以执行你不需要太多的任务。为你工作。 你发表了评论

  

如果我创建100个按钮

,我怎么能记住

回答其中一个问题,其中突出显示导致SIGBART问题的原因。 UIGestureRecognizer没有标签属性。

这是你可以做的,是迭代你的[自我视图]的所有子视图,找到具有相同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中跟踪你的UIButton,你可以通过创建一个类属性(或成员变量)并使用它来添加按钮来实现这一点。 NSMutableArray的'addObject:'函数。然后代替行

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

以上你可以交换

for( UIButton* pButton in m_pMutableButtonArray )

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

那应该解决你的问题。

答案 3 :(得分:0)

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

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents