2012-09-19 60 views
1

我正在使用自定義視圖,並有一個UIButton,但它不工作。當然,該按鈕位於自定義視圖內。這讓我瘋狂。我嘗試了self.isUserInteractionEnabled = YES ;,但得到一個錯誤「沒有setter方法」setIsUserInteractionEnabled:「分配」。任何人都可以幫助...UIButton不工作在自定義UIImageView

我的頭文件...

#import <Foundation/Foundation.h> 
#import "Clone.h" 

@class UICloneInfoView; 

@interface UICloneInfoView : UIImageView 
{ 
    Clone *clone;  
} 

@property(nonatomic, retain) Clone *clone; 
@property(nonatomic, retain) UIButton *button; 
@property(nonatomic, retain) UILabel *infoLabel; 

// set a clone to show information on the bar 
- (void)setClone:(Clone *)aClone; 

@end 

這裏是.m文件...

#import "UICloneInfoView.h" 

#define DEFAULT_HEIGHT_VIEW  90.0f 
#define DEFAULT_WIDTH_VIEW  819.0f 
#define DEFAULT_BUTTON_SIZE  40.0f 

@implementation UICloneInfoView 
@synthesize clone = _clone; 
@synthesize button = _button; 
@synthesize infoLabel = _infoLabel; 

- (id)initWithImage:(UIImage *)image 
{ 
    self = [super initWithImage:image]; 

    if (self) { 
     // init somthing 

     // init info label 
     _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, DEFAULT_WIDTH_VIEW - 80.0f, DEFAULT_HEIGHT_VIEW)]; 
     _infoLabel.backgroundColor = [UIColor clearColor]; 
     _infoLabel.font = [UIFont boldSystemFontOfSize:13.0f]; 
     _infoLabel.lineBreakMode = UILineBreakModeCharacterWrap; 
     _infoLabel.numberOfLines = 3; 

     // init button 
     _button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     _button.frame = CGRectMake(_infoLabel.frame.origin.x + _infoLabel.frame.size.width + 10.0f, (self.frame.size.height/2), DEFAULT_BUTTON_SIZE, DEFAULT_BUTTON_SIZE); 
     [_button setTitle:@"1" forState:UIControlStateNormal]; 
     [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:_button]; 
    } 

    return self; 
} 

- (void)setClone:(Clone *)aClone 
{ 
    _clone = aClone; 

    // set label text 
    _infoLabel.text = [NSString stringWithFormat:@"Start Line: %d\tEnd Line: %d\t nLines: %d\tpcid: %d\nFile: %@", _clone.startLine, _clone.endLine, _clone.endLine - _clone.startLine + 1, _clone.pcid, _clone.sourcePath]; 

    // adjust the label with new height  
    CGSize expectedLabelSize = [_infoLabel.text sizeWithFont:_infoLabel.font constrainedToSize:_infoLabel.frame.size lineBreakMode:_infoLabel.lineBreakMode]; 
    CGRect newFrame = _infoLabel.frame; 
    newFrame.size.height = expectedLabelSize.height; 
    _infoLabel.frame = newFrame; 

    // add _infoLabel to view 
    [self addSubview:_infoLabel]; 
} 

- (void)buttonAction:(id)sender 
{ 
    NSLog(@"%@", ((UIButton*)sender).titleLabel.text); 
} 

@end 

這裏是一個快照 enter image description here

謝謝提前。

回答

2

默認情況下,UIImageView的userInteractionEnabled設置爲NO。您將該按鈕添加爲圖像視圖的子視圖。你應該把它設置爲YES。

+1

在initWithImage方法中設置self.userInteractionEnabled = YES +10 –

+0

非常感謝... – Avigit

0

這可能是因爲你的類是UIImageView的子視圖,所以圖像視圖處理所有觸摸事件,並且它們永遠不會到達你的按鈕。您可以在圖像視圖中禁用用戶交互,以便觸摸事件進入按鈕。

一個更簡單的解決方案就是將UICloneInfoView改爲UIView的一個子類。然後你可以添加你的按鈕,你的標籤,克隆,然後添加一個UIImageView作爲子視圖。

是否有任何理由想要繼承UIImageView?我不知道你的要求。