2011-10-14 46 views
0

夥計們,我以爲我找到了解決方案,但沒有....我讓應用程序繼續。從過去的16小時,這可能是我的空虛的心靈沒有發出任何好聲音... plz幫助我..點擊圖像我得到EXC_BAD_ACCESS

我設置標籤到我的動態生成的圖像並設置tapgestureReconizers對此,標籤被給予UITapGestureRecognizer ,我想要點擊不同的圖像執行不同的操作,但是當點擊任何圖像時,它會非常默默地說:... EXC_BAD_ACCESS。 代碼: -

#import <UIKit/UIKit.h> 

@class AppDelegate_iPhone,Litofinter,ParsingViewController; 

@interface FirstViewController : UIViewController<UIGestureRecognizerDelegate>{ 

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

    ParsingViewController *obj; 

    UIScrollView *scrollView; 

    NSMutableArray *idArray; 


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

-(void)onTapImage:(UITapGestureRecognizer *)recognizer; 
@end 




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

@implementation FirstViewController 

@synthesize scrollView; 

-(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); 


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

     [scrollView addSubview:imgView]; 

     tgr.view.tag =(int)[NSString stringWithFormat:@"%@",lito.cId]; 
     NSLog(@"Tag Id = %@",tgr.view.tag); 
    // NSLog(@"Tag Id = %@",lito.cId); 



     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; 
     } 

     //[tgr release]; 
     [imgView release]; 

    } 
    [self.view addSubview:scrollView]; 


} 

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

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

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



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


@end 
+0

我的應用程序獲取EXC_BAD_Access - (void)onTapImage:(UITapGestureRecognizer *)識別器; { – mAc

回答

3

這可能是由於您嘗試登錄一個int作爲一個字符串在-(void)onTapImage:(UITapGestureRecognizer *)recognizer選擇:

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

代替,替換%@%i

NSLog(@"Tapped Image tag: %i", recognizer.view.tag); 
+0

哦!很好,但我的ID正在變化2011-10-14 20:31:29.030 ImageTest [5163:207]標籤ID = 22 2011-10-14 20:31:29.031 ImageTest [5163:207]標籤ID = 22 當前語言:汽車;當前目標-c 2011-10-14 20:31:31.862 ImageTest [5163:207]點擊圖片標籤:82283184 – mAc

+0

ImageTest [5163:207]標籤ID = 22 ImageTest [5163:207]點擊圖片標籤:82283184 – mAc

+0

嘗試類似於:'NSLog(@「Tapped Image tag:%@」,[recognizer.view.tag intValue]);' – FreeAsInBeer