2011-10-14 5 views
1

XML을 구문 분석하여 동적으로 생성 된 여러 이미지 뷰에서 다른 작업을 수행하려고합니다. 하지만 imageviews는 블록에 만들어 지므로 태그를 뷰와 연관 시키므로 LAST 태그는 모두 동일하게 나타납니다. 어떤 도움을어떤 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.view이미지 뷰이다.

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

당신은 또한'UIGestureRecognizer 변경해야 함을 잊지 그나마 도움이 될 수 있습니다 'init 메소드를 호출한다. UITapGestureRecognizer * tgr = [[UITapGestureRecognizer alloc] initWithTarget : 자기 행동 : @selector (onTapImage :)]; ' –

+0

;-) 추가 (동시 발생) – EmptyStack

+0

@ 빈 스택과 펠리페 사비노 ..... . 많은 친구들에게 감사드립니다. U r 내게 잘 먹었습니다. : D .. Thanks :) – mAc

1

당신은 거의 모든 잘 한 다음 줄의 선택 onTapImage에 콜론 (:)를 추가하는 것을 잊지 마십시오.

고유 태그를 UITapGestureRecognizer : tgr.tag = uniqueTag;으로 설정하고 방법 선언을 -(void)onTapImage:(UITapGestureRecognizer *)recognizer으로 바꿉니다. 그런 다음

당신이 -(void)onTapImage:(UITapGestureRecognizer *)recognizer에 해당 태그에 의해 도청 이미지를 감지 할 수있을 것입니다 : 대신 UILabel의의있는 UIButton를 사용

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

@ Nekto .... Thanks Bro – mAc

+0

하지만 내 코드는 나쁜 액세스라고 말하고 있습니다. : ( – mAc

+0

제스처를 imageview에 하위보기로 추가하기 전에 태그를 제공하고 있습니다 ... 하지만 그 후에도 내 코드는 exec BAd 액세스입니다. :(모든 도움 ... – mAc

1

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

하지만 내 코드는 나쁜 액세스라고 말합니다. :( – mAc

관련 문제