how to get the tag of the UIImageView I#39;m tapping?(如何获取我正在点击的 UIImageView 的标签?)
问题描述
我有几个 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];
显示图像.
我确实标记了所有这些,问题是我如何找出我正在点击的 imageView 的 tag
?谢谢阅读^_^
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)sender 方法正在被调用,你所要做的就是:
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 的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取我正在点击的 UIImageView 的标签?


- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- URL编码Swift iOS 2022-01-01
- UITextView 内容插图 2022-01-01