iPhone: How to know which object has been touched

advertisements

On my UIViewController I have different UIViews, and some of them are my custom UIViews. How to know which UIView was touched, my custom or not ??


You can set tag to each of your view.

view1.tag = y;
UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(singleTapGestureCaptured:)];
tapGesture.numberOfTapsRequired = 1;
[view1 addGestureRecognizer:tapGesture];

and in singleTapGestureCaptured method:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    [[gesture view] tag];
    NSLog(@"tap captured for view :%d", [[gesture view] tag]);
}