Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从自定义UIControl子类转发事件_Iphone_Events_Subclass_Uicontrol - Fatal编程技术网

Iphone 从自定义UIControl子类转发事件

Iphone 从自定义UIControl子类转发事件,iphone,events,subclass,uicontrol,Iphone,Events,Subclass,Uicontrol,我的自定义子类扩展UIControl(可编辑图像视图) 它基本上包含3个子视图: UIButton(UIButtonTypeCustom) UIImageView UILabel 所有的类都工作得很好,但是现在我想把这个类生成的一些事件连接到另一个类。尤其是按下按钮时,我想将事件转发给所有者viewcontroller,以便处理事件。问题是我不知道如何实现这种行为。 在EditableMageView中,我可以使用[button addTarget:self action:@selector

我的自定义子类扩展UIControl(可编辑图像视图) 它基本上包含3个子视图:

  • UIButton(UIButtonTypeCustom)
  • UIImageView
  • UILabel
所有的类都工作得很好,但是现在我想把这个类生成的一些事件连接到另一个类。尤其是按下按钮时,我想将事件转发给所有者viewcontroller,以便处理事件。问题是我不知道如何实现这种行为。 在EditableMageView中,我可以使用[button addTarget:self action:@selector(button Pressed:)forControlEvents:UIControlEventTouchUpInside]捕捉触摸事件,但我不知道如何在button Pressed selector内转发它。 我也试着实现touchsbegind,但似乎从未调用过

我希望通过以下方式从viewcontroller捕获按钮按下事件:

- (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];
}

这是我的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;
}
谢谢。

我是这样解决的:

- (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];
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
 (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];
}
- (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!");
}
EditableImageView.m:

@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
 (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];
}
- (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!");
}
MyController.m:

@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
 (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];
}
- (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!");
}