BlockでUIBttonのクリックを書き換える

1277 ワード

@UIButtonにサブクラスを書いて、クリックの効果を実現して、しかし1つの問題があって、buttonのtypeを設定することができなくて、背景の色にbuttonのtitleが見えないで、もし誰かが改善したら、意見の伝言を求めます
#import <UIKit/UIKit.h>

@class HMTBlockButton;
typedef void(^BlockButton)(HMTBlockButton *);

@interface HMTBlockButton : UIButton

@property (nonatomic,copy)BlockButton blockButton;

@end

#import "HMTBlockButton.h"

@implementation HMTBlockButton

- (void)dealloc{

    Block_release(_blockButton);
    [super dealloc];

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addTarget:self action:@selector(onClickButton) forControlEvents:UIControlEventTouchUpInside];
       
    }
    return self;
    
}


- (void)onClickButton{

    _blockButton(self);

}
    HMTBlockButton _button = [[HMTBlockButton alloc]initWithFrame:CGRectMake(110, 510, 100, 50)];  
    _button.backgroundColor = [UIColor redColor];  
    [_button setTitle:@" " forState:UIControlStateNormal];  
    [self.view addSubview:_button];  
    [_button release];  
      
    //  Button Block   
    _button.blockButton = ^(HMTBlockButton * button){  
    
        /** 
         *  ....... 
         *  
         */  

    };