2011-10-14 45 views
1

我想执行其被解析XML动态创建不同的图像视图不同的动作。但imageviews在一个块中创建的,所以我将其与关联意见的标签是没有用的,因为他们都来到这是最后的标签相同。 任何帮助将不胜感激... :)如何知道我正在点击哪个ImageView?

代码: -

@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 

回答

2

正确的方法是做到这一点。

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

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

这里recognizer.viewImageView的。不要忘记冒号(:)在下面的行添加到选择onTapImage

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

不要忘记,你也应该改变'UIGestureRecognizer 'init方法,因为选择器已经改变。 'UITapGestureRecognizer * TGR = [[UITapGestureRecognizer的alloc] initWithTarget:自动作:@selector(onTapImage :)];' –

+0

;-)添加(在同一时间???) – EmptyStack

+0

@空栈和菲利普萨比诺..... .Thanks很多朋友....ü[R非常适合我ATLEAST ...:d ..谢谢:)从我 – mAc

1

你已经做了几乎所有的好。

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

然后,你将能够探测到由标签拍了拍图像中-(void)onTapImage:(UITapGestureRecognizer *)recognizer

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

@Nekto ....谢谢兄弟 – mAc

+0

但我的代码是说Exec的错误访问.. :( – mAc

+0

我给标签添加手势子视图ImageView的前...但之后,也是我的代码是坏的exec访问.. :(任何帮助...... – mAc

1

使用替代的UILabel的UIButton可能有助于

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

但我的代码是说Exec的错误访问.. :( – mAc

相关问题