iOS子类化UIControl没有响应

时间:2012-04-17 05:21:40

标签: objective-c cocoa-touch uibutton uicontrol

我正在为子类化uiControl进行测试,但它还没有工作,

这里是我的代码:

MainVC.m ::

- (void)viewDidLoad
{
    [super viewDidLoad];

    TesteaContol *tt = [[[TesteaContol alloc]initWithFrame:CGRectMake(20, 20, 80, 30)]autorelease];
    [tt addTarget:self action:@selector(ttActiono:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tt];
}

- (void)ttActiono:(TesteaContol*)message
{
    NSLog(@"proyection");
}

TesteaContol.h

@interface TesteaContol : UIControl

TesteaControl.m

#import "TesteaContol.h"

    @implementation TesteaContol

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code

            NSLog(@"cao");

            UIButton *vacas = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [vacas setTitle:@"pingus" forState:UIControlStateNormal];   ;
            vacas.titleLabel.textColor = [UIColor blackColor];

            vacas.frame = CGRectMake(0, 0, 80, 40);
            [vacas addTarget:self action:@selector(vacasPressed:) forControlEvents:UIControlStateNormal];

            [self addSubview:vacas];
        }
        return self;
    }


    - (void) vacasPressed:(id) sender
    {
        NSLog(@"vacanio");

        [self sendActionsForControlEvents:UIControlEventTouchUpInside];

    }

所以我正在使用uiButton进行测试,我看到按钮,但没有响应触摸,

我错过了什么?

谢谢!

1 个答案:

答案 0 :(得分:3)

这里有一个问题:

[vacas addTarget:self action:@selector(vacasPressed:) 
    forControlEvents:UIControlStateNormal];

“forControlEvents”部分应为UIControlEvents类型,UIControlStateNormal不是。对于常规按钮,您希望将UIControlStateNormal替换为UIControlEventTouchUpInside

[vacas addTarget:self action:@selector(vacasPressed:)
    forControlEvents:UIControlEventTouchUpInside];