在我的iPhone应用程序视图中,我添加了一个子视图,这是一个自定义类。我有这个自定义类的.h和.m文件,它继承自Image View。
问题是触摸开始我的viewcontroller的方法被调用,而不是自定义类的触摸开始方法。
我在想的是当我在视图中触摸image class view
时:
/* Assign Categories */
Category *category1=[[Category alloc] initWithName:[[categoriesMArray objectAtIndex:0] valueForKey:@"category_name"] identity:[[[categoriesMArray objectAtIndex:0] valueForKey:@"category_id"] intValue] imageName:[[categoriesMArray objectAtIndex:0] valueForKey:@"category_image"]];
[category1 setFrame:CGRectMake(10,10, 150, 150)];
Category *category2=[[Category alloc] initWithName:[[categoriesMArray objectAtIndex:1] valueForKey:@"category_name"] identity:[[[categoriesMArray objectAtIndex:1] valueForKey:@"category_id"] intValue] imageName:[[categoriesMArray objectAtIndex:1] valueForKey:@"category_image"]];
[category2 setFrame:CGRectMake(170,10, 150, 150)];
NSLog(@"Categories %@",category3.imageName);
[self.view addSubview:category1];
[self.view addSubview:category2];
[self.view bringSubviewToFront:category1];
它应该调用我的自定义类部分:
@implementation Category
@synthesize categoryName,categoryID,imageName;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithName:(NSString *)name identity:(int)index imageName:(NSString *)imgName
{
if ((self = [super init])) {
self.categoryName = name;
self.categoryID =index;
self.imageName=imgName;
UIImage *temptoResize=[UIImage imageNamed:self.imageName];
[super setImage:[temptoResize scaleToSize:CGSizeMake(200, 150)]];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Category Touch %@",self.frame);
}
// ------------------------
自定义类的 -touchesBegan method
而不是查看控制器。
请告诉我为什么我的自定义类方法没有被调用。
答案 0 :(得分:7)
好的,在你的
中- (id)initWithName:(NSString *)name identity:(int)index...
方法,添加以下行
self.userInteractionEnabled = YES;
因为默认为NO。请参阅UIImageView Apple文档。