manuelBetancurt
manuelBetancurt

Reputation: 16128

iOS subclassing UIControl not responding

im doing a test for subclassing uiControl, but it is not working yet,

here my code:

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

    }

so I am testing with a uiButton, i see the button, but is not responding to the touches,

what im i missing?

thanks!

Upvotes: 0

Views: 883

Answers (1)

Cowirrie
Cowirrie

Reputation: 7226

There is a problem here:

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

The "forControlEvents" part should be of type UIControlEvents which UIControlStateNormal is not. For a regular button, you want to replace UIControlStateNormal with UIControlEventTouchUpInside:

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

Upvotes: 3

Related Questions