点击图像我得到EXC_BAD_ACCESS

时间:2011-10-14 14:56:43

标签: iphone objective-c

伙计们,我以为我找到了解决方案,但没有....我正在制作app cont。从过去的16个小时开始,可能是我的空虚心灵没有发出任何好听的声音......请帮助我......

我将Tag设置为动态生成的图像视图并设置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

1 个答案:

答案 0 :(得分:3)

这可能是因为您尝试将int作为字符串记录在-(void)onTapImage:(UITapGestureRecognizer *)recognizer选择器中:

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

而是将%@替换为%i

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