Objective c UIButton在自定义UIImageView中不工作

Objective c UIButton在自定义UIImageView中不工作,objective-c,uiview,uibutton,custom-view,Objective C,Uiview,Uibutton,Custom View,我使用的是自定义视图,其中有一个UIButton,但它不起作用。当然,按钮在自定义视图中。这快把我逼疯了。我尝试了self.isUserInteractionEnabled=YES;,但获取错误“No setter method'setIsUserInteractionEnabled:'for assignment”。有人能帮忙吗 我的头文件 #import <Foundation/Foundation.h> #import "Clone.h" @class UICloneInfo

我使用的是自定义视图,其中有一个UIButton,但它不起作用。当然,按钮在自定义视图中。这快把我逼疯了。我尝试了self.isUserInteractionEnabled=YES;,但获取错误“No setter method'setIsUserInteractionEnabled:'for assignment”。有人能帮忙吗

我的头文件

#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
这是一张快照


提前感谢。

这可能是因为您的类是UIImageView的子视图,因此图像视图正在处理所有触摸事件,并且它们永远不会到达您的按钮。您可以在图像视图上禁用用户交互,以便触摸事件到达按钮

一个简单得多的解决方案是将UICloneInfo改为UIView的子类。然后,您可以添加按钮、标签、克隆,然后仅添加UIImageView作为子视图


是否有任何理由要将UIImageView子类化?我不知道您的要求。

UIImageView默认将userInteractionEnabled设置为否。您正在将按钮作为子视图添加到图像视图中。您应该将其设置为YES。

在initWithImage方法+10中设置self.userInteractionEnabled=YES
#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