在我的 iPhone 应用程序 View 中,我添加了一个 subview ,它是一个自定义类。我有这个自定义类的 .h 和 .m 文件,它继承自 Image View。
问题是调用了我的 View Controller 的触摸开始方法,而不是自定义类的触摸开始方法。
我在想的是当我触摸这个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
我的 自定义类 而不是我的 查看 Controller 的 .请告诉我为什么不调用我的自定义类方法。
最佳答案
好的,在你的
- (id)initWithName:(NSString *)name identity:(int)index...
方法,添加以下行
self.userInteractionEnabled = YES;
因为它默认为 NO。见 UIImageView苹果文档。
关于iphone - 我的自定义类中未调用 Touches Began 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8784137/