按钮是动态创建的,图像是通过解析的URL给出的,所以我在viewDidLoad中创建它们并仅在viewDidLoad中释放它们。所以,当我点击按钮时,我想点击不同的按钮执行不同的任务。但我无法做到。 我试图将标签设置为他们,但再次没有用,因为它给了我EXC_BD_ACCESS ....任何帮助将不胜感激... :) 代码: -
@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];
}
答案 0 :(得分:1)
您可以在设置目标
时根据按钮更改选择器[imageButton addTarget:self action:@selector(onTapButton:) forControlEvents:UIControlEventTouchUpInside];
如果你想用标签做,它也可以工作,你的不良访问来自
NSLog(@"Tag Id = %@",self.view.tag);
假设该标签是NSObject(响应描述选择器),实际上它是一个数值。将其更改为:
NSLog(@"Tag Id = %i",self.view.tag);
另外,使用
时要小心NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
因为这是一个同步API,所以如果它是本地文件就可以了(尽管使用[UIImage imageNamed:]比较缓存图像数据更经济),如果它是网络文件(因为它阻塞线程直到请求结束,这可能需要很长时间)。