如何获取我正在点击的 UIImageView 的标签? [英] how to get the tag of the UIImageView I'm tapping?

查看:18
本文介绍了如何获取我正在点击的 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 的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆