Reputation: 8338
I have a view controller where there are several uiview object. I need to know on which uiview user have tapped. how is this possible? any guidance will help a lot....
Thanks
Pankaj
Upvotes: 4
Views: 1028
Reputation: 8383
Here is what you can do to get what you wanted ..... In this example i have 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 the 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 should help you
Upvotes: 6
Reputation: 774
Set a tag for every view to keep track of 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);
}
Upvotes: 1
Reputation: 26390
U can probably add a custom button with a tag on top of each view. Then u can know which view is tapped based on the button tag.
pls take a look at this. It may help.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13041-touch-event-subview.html
Upvotes: 0
Reputation: 10011
you can add gestures to the uiview objects to find which object has been touched. see the documentation.
for specific code just comment.
Upvotes: 0