2012-04-17 73 views
0

即時通訊做了繼承uiControl測試,但還沒有成型,iOS的子類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測試中,我看到的按鈕,但不響應觸摸,

我錯過了什麼IM?

謝謝!

回答

3

這裏有一個問題:

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

的 「forControlEvents」 部分應該是UIControlEvents類型的UIControlStateNormal不是。對於常規按鈕,要將UIControlStateNormal替換爲UIControlEventTouchUpInside

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