开发者

find which view was clicked

I have a view controller where there are several uiview object. I need to know on which uiview user开发者_如何学Python have tapped. how is this possible? any guidance will help a lot....

Thanks

Pankaj


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


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);
}


you can add gestures to the uiview objects to find which object has been touched. see the documentation.

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

for specific code just comment.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜