Reputation: 2514
Buttons are created dynamically and images are given to them through parsed urls, so i viewDidLoad i created them and released them in viewDidLoad only. So, when i click on Buttons i want to perform different tasks on click on different buttons. but i am not able to do it. I tried to set Tag to them but again it is of no use as it is giving me EXC_BD_ACCESS.... any Help would be appreciated... :) Code:-
@class AppDelegate_iPhone,Litofinter,ParsingViewController;
@interface FirstViewController : UIViewController<UIGestureRecognizerDelegate>{
NSMutableArray *array;
NSString *logoString;
AppDelegate_iPhone *appDelegate;
ParsingViewController *obj;
UIScrollView *scrollView;
NSMutableArray *idArray;
NSMutableArray * currentPdfUrl;
}
@property (nonatomic,retain)UIScrollView *scrollView;
//-(void)onTapImage:(UITapGestureRecognizer *)recognizer;
-(void)onTapButton:(id)sender;
@end
#import "FirstViewController.h"
#import "AppDelegate_iPhone.h"
#import "Litofinter.h"
#import "ParsingViewController.h"
#import "MyGesture.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);
*/
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(x, y, 140, 90)];
[imageButton setImage:image forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(onTapButton:) forControlEvents:UIControlEventTouchUpInside];
[imageButton setTag:lito.cId];
[scrollView addSubview:imageButton];
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)onTapButton:(id)sender
{
NSLog(@"Tag Id = %@",self.view.tag);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message from mAc" message:@"Tag Id" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[alert show];
}
Upvotes: 1
Views: 161
Reputation: 16827
You could change the selector depending on the button when setting the target
[imageButton addTarget:self action:@selector(onTapButton:) forControlEvents:UIControlEventTouchUpInside];
If you want to do it with tags, it could also work, your bad access is coming from
NSLog(@"Tag Id = %@",self.view.tag);
Which is assuming that tag is an NSObject (responding to the description selector), when in fact, it is a numeric value. Change it to:
NSLog(@"Tag Id = %i",self.view.tag);
Also, you want to be careful when using
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
as this is a synchronous API, it's ok if it's local files (although using [UIImage imageNamed:] is more economical as it caches the image data), it's really bad if it's network files (as it blocks the thread until the request ends, which could take ages).
Upvotes: 1