开发者

Tapping on image i m getting EXC_BAD_ACCESS

开发者 https://www.devze.com 2023-04-13 05:47 出处:网络
Guys, i thought i found the solution but no.... i making app cont. from past 16 hrs, may be that\'s y my empty mind is not making any good sound... plz help me..

Guys, i thought i found the solution but no.... i making app cont. from past 16 hrs, may be that's y my empty mind is not making any good sound... plz help me..

I am setting Tag to my dynamically generated imageviews and setting tapgestureReconizers on that,tags are given to UITapGestureRecognizer, i want to perform different actions on click on different images, but when ever click on any image it says very silently... :| EXC_BAD_ACCESS. Code: -

#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] init开发者_如何学JAVAWithArray: 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


This could be due to you trying to log an int as a string in your -(void)onTapImage:(UITapGestureRecognizer *)recognizer selector:

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

instead, replace %@ with %i:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号