如何获取我正在点击的UIImageView的标签? [英] how to get the tag of the UIImageView I'm tapping?
问题描述
我有几个UIImageView,每个都有一个标签;我有一个图像数组,我想要做的是:当用户点击其中一个UIImageView时,应用程序从数组中返回特定的图像。
i have several UIImageView, each of them has a tag; and I have an array of Images, what i want to do is: when user tap one of the UIImageView, the app give back the certain Image from array.
我实现像这样:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<8; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
imageView.backgroundColor = [UIColor blueColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(@"%d", imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 115*i);
}
- (void)findOutTheTag:(id)sender
{
// HOW TO FIND THE tag OF THE imageView I'M TAPPING?
}
我想找出 imageView .tag
,并将 imageView.tag
传递给
UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag];
显示图像。
I确实标记了所有这些,问题是如何找到我正在点击的imageView的标签
?感谢您阅读^ _ ^
I did tag all of them, question is how I can find out the tag
of the imageView I'm tapping? thank you for reading ^_^
推荐答案
如果没有看到您应用的完整图片,可能会提出建议,为什么不呢?为此使用自定义UIButton而不是UIImageViews?使用UIButton,您可以设置操作并传递发件人ID,您可以从中轻松访问您的代码并从数组中提取数据。
At the risk of making a recommendation without seeing the full picture of your app, why not use a custom UIButton instead of UIImageViews for this? With a UIButton you can setup an action and pass the sender id, from which you can easily access your tags and pull the data from your array.
或者如果您真的想要使用上面的代码和你知道的事实 - (void)findOutTheTag:(id)发送方法被调用,你所要做的就是:
OR if you really want to use the above code and your know for a fact the - (void)findOutTheTag:(id)sender method is being called, all your have to do is:
- (void)findOutTheTag:(id)sender {
switch (((UIGestureRecognizer *)sender).view.tag)
{
case kTag1:
//...
case kTag2:
//...
}
}
这篇关于如何获取我正在点击的UIImageView的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!