Question

I am using a custom view and there is a UIButton in it but it is not working. Of course the button is inside the custom view. It's driving me crazy. I tried by self.isUserInteractionEnabled = YES;, but getting an error "No setter method 'setIsUserInteractionEnabled:' for assignment". Can anyone help...

My header file...

#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

Here is the .m file...

#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

Here is a snapshot enter image description here

Thanks in advance.

Was it helpful?

Solution

UIImageView has userInteractionEnabled set to NO by default. You are adding the button as a subview to the image view. You should set it to YES.

OTHER TIPS

It might be because your class is a subview of UIImageView, so the image view is handling all the touch events, and they are never reaching your button. You could play around with disabling user interaction on the image view so that the touch event gets to the button.

A far simpler solution would just be to make UICloneInfoView a subclass of UIView instead. Then you can add your button, your labels, Clone and then just add a UIImageView as a subview.

Is there any reason you wanted to subclass UIImageView? I don't know your requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top