我怎么知道我正在点击哪个Imageview?

时间:2011-10-14 12:16:13

标签: iphone objective-c

我想对通过解析XML动态创建的不同图像视图执行不同的操作。但由于图像视图是在一个块中创建的,因此我将它与视图相关联的标记没有用,因为它们都是相同的LAST标记。 任何帮助将不胜感激...... :)

代码: -

@class AppDelegate_iPhone,Litofinter,ParsingViewController;

@interface FirstViewController : UIViewController {

    NSMutableArray *array;
    NSString *logoString;
    AppDelegate_iPhone *appDelegate;

    ParsingViewController *obj;

    UIScrollView *scrollView;

    NSMutableArray *idArray;

//  UIImageView *imgView;

}
@property (nonatomic,retain)UIScrollView *scrollView;
//@property (nonatomic,retain)UIImageView *imgView;

-(void)onTapImage;
@end







#import "FirstViewController.h"
#import "AppDelegate_iPhone.h"
#import "Litofinter.h"

#import "ParsingViewController.h"

@implementation FirstViewController

@synthesize scrollView;//,imgView;

-(id)init{
    if(self == [super init]){
        obj = [[ParsingViewController alloc] init];
        array = [[NSArray alloc] initWithArray: obj.LogoMutableArray];
    }
    return self; 
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    int x=20,y=10;
    int a=50,b=105;


    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,500, 460)];   
    scrollView.contentSize = CGSizeMake(320,800);
    scrollView.showsVerticalScrollIndicator = YES;

    for (Litofinter *lito in appDelegate.logoArray) {

        NSString * urlString = [lito.cLogo stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        NSURL * imageURL = [NSURL URLWithString:urlString];

        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:imageData];

        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        [imgView setFrame:CGRectMake(x, y, 140, 90)];
        imgView.userInteractionEnabled = YES;
        imgView.multipleTouchEnabled = YES;
        imgView.backgroundColor = [UIColor blueColor];
        imgView.tag = lito.cId;
        NSLog(@"Tag Id = %@",imgView.tag);
        NSLog(@"Tag Id = %@",lito.cId);

        [scrollView addSubview:imgView];


        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage)];
        [imgView addGestureRecognizer:tgr];
        [tgr release];
        //[imgView release];

        UILabel *cName = [[UILabel alloc]initWithFrame:CGRectMake(a, b, 130, 20)];
        cName.text = lito.cName;
        [scrollView addSubview:cName];

        //Do the rest of your operations here, don't forget to release the UIImageView
        x = x + 150;
        a = a + 140;

        if(x >300)
        {
            y = y +140;
            x = 20;
            b = b +150;
            a = 50;
        }

    //  idArray = [[NSMutableArray alloc]init];
    //  [idArray addObject:lito.cId];

    }
    [self.view addSubview:scrollView];


}


-(void)onTapImage
{

    NSLog(@"Tapped Image Id ======================== %@",view.tag);
    //UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message from mAc" message:[NSString stringWithFormat:@"Tag Id : %@",imgView.tag] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
    //[alert show];
}



- (void)dealloc {
    [super dealloc];
    [scrollView release];
}
@end

3 个答案:

答案 0 :(得分:2)

正确的方法是这样做。

- (void)onTapImage:(UITapGestureRecognizer *)recognizer {

    NSLog(@"Tapped Image tag: %d", recognizer.view.tag);
}

此处 recognizer.view imageView 。不要忘记将冒号(:)添加到以下行中的选择器 onTapImage

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage:)];

答案 1 :(得分:1)

你几乎做得很好。

只需将唯一标记设置为UITapGestureRecognizertgr.tag = uniqueTag;并将您的方法声明替换为-(void)onTapImage:(UITapGestureRecognizer *)recognizer

然后,您将能够在-(void)onTapImage:(UITapGestureRecognizer *)recognizer中检测到该标记所点击的图像:

NSLog(@"Tapped Image Id ======================== %d", recognizer.tag);

答案 2 :(得分:1)

使用UIButton而不是UILabel可能会有所帮助

imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.userInteractionEnabled = YES;
aButton.frame = imageView.frame;
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];