2011-10-14 49 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

相關問題