Forward event from a custom subclass of UIControl

My custom subclass extends UIControl (EditableImageView) It basically contains 3 subheadings:

  • UIButton (UIButtonTypeCustom)
  • UIImageView
  • Uilabel

The whole class works fine, but now I want to relate some events created from this class to another. In particular, when I click the button, I want to forward the event to the owner view manager so that I can handle the event. The problem is that I cannot figure out how to implement this behavior. In EditableImageView, I can catch the touch event using [button addTarget: self action: @selector (buttonPressed :) forControlEvents: UIControlEventTouchUpInside], but I don't know how to redirect it inside the button. Press the selector. I also tried to implement touchhesBegan, but it seems to have never been called ...

I would like to capture the button click event from the viewcontroller like this:

- (void)viewDidLoad { [super viewDidLoad]; self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)]; [imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:imageButton]; [imageButton setEditing:NO]; 

}

This is my method of initializing a subclass of UIControl:

 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setBackgroundColor:[UIColor clearColor]]; button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height); [button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; transparentLabelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"editLabelBackground.png"]]; transparentLabelBackground.hidden = YES; [self addSubview:transparentLabelBackground]; // create edit status label editLabel = [[UILabel alloc] initWithFrame:CGRectZero]; editLabel.hidden = YES; editLabel.userInteractionEnabled = NO; // without this assignment the button will not be clickable editLabel.textColor = [UIColor whiteColor]; editLabel.backgroundColor = [UIColor clearColor]; editLabel.textAlignment = UITextAlignmentLeft; UIFont *labelFont = [UIFont systemFontOfSize:16.0]; editLabel.font = labelFont; editLabel.text = @"edit"; labelSize = [@"edit" sizeWithFont:labelFont]; [self addSubview:editLabel]; } return self; } 

Thanks.

+4
source share
1 answer

I decided this way:

EditableImageView.h:

 @interface EditableImageView : UIControl { UIButton *button; UIImageView *transparentLabelBackground; UILabel *editLabel; CGSize labelSize; BOOL editing; } @property (nonatomic, retain) UIButton *button; @property (nonatomic, retain) UILabel *editLabel; @property (nonatomic, retain) UIImageView *transparentLabelBackground; @property (nonatomic, getter=isEditing) BOOL editing; - (void)buttonPressed:(id)sender; @end 

EditableImageView.m:

  (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setBackgroundColor:[UIColor clearColor]]; button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height); [button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; ....... } } - (void)buttonPressed:(id)sender { [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } 

MyController.m:

 - (void)viewDidLoad { [super viewDidLoad]; self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)]; [imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:imageButton]; } - (void)buttonPressed:(id)sender { NSLog(@"buttonPressed!"); } 
+6
source

Source: https://habr.com/ru/post/1310687/


All Articles