开发者

How can i know on which Imageview i am tapping?

I want to perform different actions on different image views which are created dynamically by parsing an XML. but as imageviews are created in a block so the tag i associate it with the views are of no use as they all came the same which is the LAST tag. Any help will be appreciated... :)

Code:-

@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 addGe开发者_Go百科stureRecognizer: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


The correct way is to do this.

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

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

Here recognizer.view is the imageView. Don't forget to add colon (:) to the selector onTapImage in the following line,

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


You have done practically all well.

Just set unique tag to UITapGestureRecognizer : tgr.tag = uniqueTag; and replace declaration of your method to -(void)onTapImage:(UITapGestureRecognizer *)recognizer.

Then you will be able to detect the image tapped by that tag in -(void)onTapImage:(UITapGestureRecognizer *)recognizer:

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


Using UIButton instead of UILabel might help

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜