iphone - 我的自定义类中未调用 Touches Began 方法

标签 iphone objective-c ios cocoa-touch

在我的 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/

相关文章:

iphone - 如何将 .ipa 文件安装到我的 iPhone 模拟器

ios - 检测 _which_ 手指触摸 iOS 设备上的屏幕

iphone - 我可以在我的应用中禁止系统警报吗?

ios - 为什么这个 UITapGestureRecognizer 无法识别点击?

iphone - 适用于 iOS 和 OS X 应用程序的库?

ios - 在 Tab Bar 前面制作 UIPickerView

iphone - CoreAudio 节奏变化 (iOS)

ios - 数据如何存储在核心数据模型(属性)中

iphone - 使用分段控件在 2 个 View 之间切换

ios - Cocos2d 返回 Sprite body 上的触摸位置