用block改寫UIButton點擊事件,block改寫UIAlerView的代理


大致就是自定義一個BlockButton繼承UIButton,然后在里面用

addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

這個方法觸發block,代碼很簡單,不多說了

BlockButton.h
@class BlockButton;
typedef void (^TouchButton)(BlockButton*);

@interface BlockButton : UIButton

@property(nonatomic,copy)TouchButton block;

@end
BlockButton.m
#import "BlockButton.h"
#import <QuartzCore/QuartzCore.h>//這里要注意,如果想使用UIButton的layer屬性更改button樣式,要添加QuartzCore.framewor並且在頭文件導入。

@implementation BlockButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10;
        self.layer.borderWidth = 2;
        self.layer.shadowRadius = 2;
        self.layer.shadowColor = [UIColor grayColor].CGColor;
        self.layer.borderColor = [UIColor redColor].CGColor;
        [self addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)touchAction:(id)sender{
    _block(self);
}

@end

ViewController.h里面沒有添加任何代碼

下面是ViewController.m

#import "ViewController.h"
#import "BlockButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    BlockButton *button = [[BlockButton alloc]initWithFrame:CGRectMake(100, 100, 40, 20)];
    [button setBlock:^(BlockButton *button){
        NSLog(@"按下去了");
    }];
    [self.view addSubview:button];
    [button release];
    
    for (int i = 0; i < 3; i ++) {
        BlockButton *btn = [[BlockButton alloc]initWithFrame:CGRectMake(40 + i*70, 200, 50, 25)];
        [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.block = ^(BlockButton *btn){
             NSLog(@"按下%d",i);
        };
        [self.view addSubview:btn];
        [btn release];
    }
}


@end

 

下面再改寫Alert這個控件,思路是一樣的,在自定義的Alert里面用block觸發點擊事件,而在Alert定義的代碼里執行事件觸發的行為。
AlertBlock.h
#import <UIKit/UIKit.h>

@class AlertBlock;
typedef void (^TouchBlock)(NSInteger);
@interface AlertBlock : UIAlertView

@property(nonatomic,copy)TouchBlock block;
//需要自定義初始化方法,調用Block
- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString*)otherButtonTitles
              block:(TouchBlock)block;
@end
AlertBlock.m
#import "AlertBlock.h"

@implementation AlertBlock

- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles block:(TouchBlock)block{
    self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];//注意這里初始化父類的
    if (self) {
        self.block = block;
    }
    return self;
}

//#pragma mark -AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//這里調用函數指針_block(要傳進來的參數);
    _block(buttonIndex);
}

@end

ViewController.m

#import "ViewController.h"
#import "AlertBlock.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 80, 30);
    [self.view addSubview:button];
}

- (void)buttonAction:(id)sender{
    AlertBlock *alert = [[AlertBlock alloc]initWithTitle:@"提示" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定" block:^(NSInteger buttonIndex){
//在這里面執行觸發的行為,省掉了代理,這樣的好處是在使用多個Alert的時候可以明確定義各自觸發的行為,不需要在代理方法里判斷是哪個Alert了
if (buttonIndex == 0) { NSLog(@"取消"); }else if (buttonIndex == 1){ NSLog(@"確定"); } }]; [alert show]; [alert release]; }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM