Find what kind of click

I have a view controller in which there are several uiview objects. I need to know what the uiview user clicked on. how is this possible? any guide will help a lot ....

thanks
Pankai

+4
source share
4 answers

Here is what you can do to get what you need ... In this example, I created 7 views

UITapGestureRecognizer* gestureRecognizer; UIView* myView; for (int i = 0; i < 8; i++) { gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomthing:)]; gestureRecognizer.numberOfTapsRequired = 1;//or what ever you want myView = [[UIView alloc] initWithFrame:CGRectMake(10, i*30, 30, 28)]; myView.backgroundColor = [UIColor redColor]; myView.tag = 100+i; [self.view addSubview:myView]; [myView addGestureRecognizer:gestureRecognizer]; [myView release]; [gestureRecognizer release]; } 

Now you need to implement a method like this

 -(void)doSomthing:(id)sender { UIView* temp = [(UITapGestureRecognizer*)sender view]; // here you get the view you wanted NSLog(@"view number :%d",temp.tag); } 

I think this will help you.

+6
source

Set a tag for each view to track them.

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // We only support single touches, so anyObject retrieves just that touch from touches UITouch *touch = [touches anyObject]; NSLog(@"view %i", [touch view].tag); } 
+1
source

you can add gestures to uiview objects to find which object was affected. see the documentation.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html

for a specific code, just a comment.

0
source

U may add a custom button with a tag on top of each view. Then u can know which view is listening based on the button tag.

Pls take a look at this. This can help.

http://www.iphonedevsdk.com/forum/iphone-sdk-development/13041-touch-event-subview.html

0
source

Source: https://habr.com/ru/post/1341265/


All Articles