4

I have two UIbuttons,and I want to implement Longpressgesture on both.

So I wrote the below code..

-(void)viewdidLoad
{
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
    longPress.minimumPressDuration = 0.5;
    [Button1 addGestureRecognizer:longPress];
    [Button2 addGestureRecognizer:longPress];

}

- (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender
{    
    if(sender.state == UIGestureRecognizerStateBegan) 
    {        

    }   
}

now my doubt is how shall I check which button is longpresses?

Thanks Ranjit

NANNAV
  • 4,875
  • 4
  • 32
  • 50
Ranjit
  • 4,576
  • 11
  • 62
  • 121

1 Answers1

6

First, note that a gesture recognizer should be attached to just one view. You should create a new instance for each button.

To answer your question, you can add tag values to your buttons:

Button1.tag = 1000;
Button2.tag = 1001;

Then test them in the recognizer:

UIView *view = sender.view;
int tag = view.tag;

if (tag == 1000) {
...
}

You can enter any tag values, but I often start at a high value like 1000 to avoid clashes with any other tags that I assign in Interface Builder.

Another option is to use a different handling function for each recognizer.

tarmes
  • 15,366
  • 10
  • 53
  • 87
  • hey thanks tarmes for your reply..hey you mean that I should create one more instance of LOngpress and add my another button to it right? – Ranjit Nov 24 '11 at 09:54
  • That's right. See here too: http://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views – tarmes Nov 24 '11 at 09:57
  • then I can right two different @selectors and my problem will be solved right? – Ranjit Nov 24 '11 at 10:07
  • That's another option, yes. It depends on your needs. – tarmes Nov 24 '11 at 10:15
  • hey tarmes,please check this link and give me some solution http://stackoverflow.com/questions/8255395/how-to-set-a-password-for-application-in-ios – Ranjit Nov 24 '11 at 10:18