2012-01-09 35 views
1

在我的iPhone應用程序中,我添加了一個子視圖,這是一個自定義類。對於從Image View繼承的此自定義類,我有.h和.m文件。觸動在我的自定義類中未調用的開始方法

問題是,觸摸開始我的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我的自定義類而不是我的視圖控件ler's

請告訴我爲什麼我的自定義類方法沒有被調用。

+0

您需要將代碼發佈到您的自定義類的初始化和touchesBegan方法 – CStreel 2012-01-09 05:07:29

+0

好吧...我會把代碼 – 2012-01-09 05:09:50

回答

7

好吧,在你

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

方法,添加以下行

self.userInteractionEnabled = YES; 

,因爲它是默認情況下沒有。請參閱UIImageView Apple文檔。

+0

再次感謝Aadhira,現在它完美的工作.... – 2012-01-09 05:34:35

+0

再次歡迎! – Ilanchezhian 2012-01-09 05:36:47

相關問題