【发布时间】:2012-01-09 05:02:50
【问题描述】:
在我的 iPhone 应用程序视图中,我添加了一个子视图,它是一个自定义类。我有这个继承自 Image View 的自定义类的 .h 和 .m 文件。
问题是我的 viewcontroller 的 touches started 方法被调用了,而不是自定义类的 touches started 方法。
我在想的是当我在我看来触摸这个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 我的自定义类,而不是我的视图控制器的。
请告诉我为什么我的自定义类方法没有被调用。
【问题讨论】:
-
您需要将代码发布到您的自定义类和您的 touchesBegan 方法的初始化中
-
好的...我会放代码
标签: iphone objective-c ios cocoa-touch