【问题标题】:Touches Began method not called in my custom class在我的自定义类中未调用 Touches Began 方法
【发布时间】: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


【解决方案1】:

好的,在你的

- (id)initWithName:(NSString *)name identity:(int)index...

方法,添加以下行

self.userInteractionEnabled = YES;

因为默认情况下它是 NO。请参阅UIImageViewApple 文档。

【讨论】:

    猜你喜欢
    • 2012-09-12
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多