如何获取我正在点击的 UIImageView 的标签?

how to get the tag of the UIImageView I#39;m tapping?(如何获取我正在点击的 UIImageView 的标签?)
本文介绍了如何获取我正在点击的 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 的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
HTTPS request using volley(使用 volley 的 HTTPS 请求)