Search code examples
ipadxcode4uigesturerecognizer

Multiple tap on UIButton and access different behavior of Button


I create a project of the Multiple user can tapped on the different button.

Following problem may phase, I implement the gestureRecognizer and it's proper work but how get the which button tapped by the user for that to access those button event's

following screen shows the button,

enter image description here Following is the code for gestureRecognizer delegate method so, how to get the button event and how to manage it,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    if ([touch.view isKindOfClass:[UIButton class]]) {
        NSLog(@"Button is pressed");
        if (tag == 1) {
            NSLog(@"Button1 is pressed");
        }
        return NO;
    }
    return YES;
}

following method for the IBAction method to touch when the button tapped

-(IBAction)btnPress:(id)sender{
    tag=[sender tag];
    NSLog(@"%i",tag);
}

But here issue is first call the gestureRecognizer delegate method then IBAction method to how to solve this problem,

Thanks in advance for your valuable time spend on my problem,

Thaks and regards Neon Samuel.


Solution

  • If the button is instance of UIButton, then you do not need to use gestureRecognizer at all. Try to set addTarget:action to get callback when user click that UIButton:

    [button1 addTarget:self action:@selector(btnPress:)];
    [button2 addTarget:self action:@selector(btnPress:)];
    

    If you have already set tag value to each button, then your IBAction method would work properly.

    -(IBAction)btnPress:(id)sender{
    NSInteger tag=[sender tag];
    NSLog(@"%d",tag);
    }