2011-02-25 95 views
4

我有一个视图控制器,其中有几个uiview对象。我需要知道哪些uiview用户已被点击。这怎么可能?任何指导将有很大的帮助....找到哪个视图被点击

感谢
潘卡

回答

6

这里是现在,你需要实现这样的

的方法,你可以做些什么来得到你想要的东西.....在这个例子中,我创建了7次

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

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

我认为这应该可以帮到你

+0

谢谢阿米特你真的教会我一些新的东西... – pankaj 2011-02-25 09:05:19

0

U很可能会在每个视图的顶部添加一个带有标记的自定义按钮。然后你就可以知道哪个视图是基于按钮标签的。

请看看这个。它可能有帮助。

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

+0

它建议我在uiview下添加一个隐形按钮。有没有其他的方式,因为它看起来很奇怪,在我的页面上不必要地添加这么多的uibutton。 – pankaj 2011-02-25 08:52:04

+0

ohkk我知道了...它花了我时间,但我发现你的意思是...谢谢 – pankaj 2011-02-25 09:01:20

1

设置标签对每个视图,以跟踪它们。

- (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); 
} 
+0

它将如何告诉我哪个视图被点击。我的页面上有大约10-12个视图。 – pankaj 2011-02-25 08:44:41

+0

对每个视图使用标记... NSLog(@“view%i”,[touch view] .tag); – FoJjen 2011-02-25 08:55:00

+0

非常感谢 – pankaj 2011-02-25 09:02:33