我想執行其被解析XML動態創建不同的圖像視圖不同的動作。但imageviews在一個塊中創建的,所以我將其與關聯意見的標籤是沒有用的,因爲他們都來到這是最後的標籤相同。 任何幫助將不勝感激... :)如何知道我正在點擊哪個ImageView?
代碼: -
@class AppDelegate_iPhone,Litofinter,ParsingViewController;
@interface FirstViewController : UIViewController {
NSMutableArray *array;
NSString *logoString;
AppDelegate_iPhone *appDelegate;
ParsingViewController *obj;
UIScrollView *scrollView;
NSMutableArray *idArray;
// UIImageView *imgView;
}
@property (nonatomic,retain)UIScrollView *scrollView;
//@property (nonatomic,retain)UIImageView *imgView;
-(void)onTapImage;
@end
#import "FirstViewController.h"
#import "AppDelegate_iPhone.h"
#import "Litofinter.h"
#import "ParsingViewController.h"
@implementation FirstViewController
@synthesize scrollView;//,imgView;
-(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);
[scrollView addSubview:imgView];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage)];
[imgView addGestureRecognizer:tgr];
[tgr release];
//[imgView release];
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;
}
// idArray = [[NSMutableArray alloc]init];
// [idArray addObject:lito.cId];
}
[self.view addSubview:scrollView];
}
-(void)onTapImage
{
NSLog(@"Tapped Image Id ======================== %@",view.tag);
//UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message from mAc" message:[NSString stringWithFormat:@"Tag Id : %@",imgView.tag] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
//[alert show];
}
- (void)dealloc {
[super dealloc];
[scrollView release];
}
@end
不要忘記,你也應該改變'UIGestureRecognizer 'init方法,因爲選擇器已經改變。 'UITapGestureRecognizer * TGR = [[UITapGestureRecognizer的alloc] initWithTarget:自動作:@selector(onTapImage :)];' –
;-)添加(在同一時間???) – EmptyStack
@空棧和菲利普薩比諾..... .Thanks很多朋友....ü[R非常適合我ATLEAST ...:d ..謝謝:)從我 – mAc