Block实战应用之button点击事件和Alert弹出框

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootVC = [[[RootViewController alloc] init] autorelease];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
    self.window.rootViewController = [nav autorelease];
    
    return YES;
}

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    WXButton *btn = [[WXButton alloc] initWithFrame:CGRectMake(100, 100, 60, 40)];
    [btn setBackgroundColor:[UIColor greenColor]];
    
    __block RootViewController *this = self;

    
    btn.btnBlock = ^{
        NSLog(@"click btn");
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        [this.navigationController pushViewController:detailVC animated:YES];
        [detailVC release];
    };
    [self.view addSubview:btn];
    
    [btn release];
}

BlockButton.h

typedef void(^ButtonBlock)();

@interface BlockButton : UIButton

@property (nonatomic, copy) ButtonBlock btnBlock;

BlockButton.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //注册事件
        [self addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)btnClick:(UIButton *)btn
{
    //调用block
    if (_btnBlock) {
        _btnBlock();
    }
    
//    //演示 @try @catch
//    NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil];
//    @try {
//        NSLog(@"%@",arr[2]);
//    }
//    @catch (NSException *exception) {
//        NSLog(@"%@",exception.name);
//        NSLog(@"%@",exception.reason);
//    }
//    @finally {
//        
//    }
}

DetailViewController.m


#import "DetailViewController.h"
#import "BlockButton.h"
#import "WXAlertView.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    
    BlockButton *btn = [[BlockButton alloc] initWithFrame:CGRectMake(100, 100, 60, 40)];
    [btn setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:btn];
    
    __block DetailViewController *this = self;
    
    btn.btnBlock = ^{
        [this.navigationController popViewControllerAnimated:YES];
    };
    [btn release];
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 150, 60, 40)];
    [btn1 setBackgroundColor:[UIColor grayColor]];
    [btn1 setTitle:@"动画" forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 release];
    
    
    WXAlertView *wxalert = [[WXAlertView alloc] initWithTitle:@"1" message:@"2" cancelButtonTitle:@"3" otherButtonTitles:@"4"];
    wxalert.alertBlock = ^(int index){
        
        if (index == 0) {
            //.....
        }
        NSLog(@"%d",index);
    };
//    [wxalert show];
    [wxalert release];
    
    WXAlertView *wxalert1 = [[WXAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"1",@"2",@"3",@"4",@"5", nil];
    
    wxalert1.alertBlock = ^(int index){
    
        NSLog(@"%d",index);

    };
    
    [wxalert1 show];
    [wxalert1 release];
    
    
    
}

- (void)btnClick:(UIButton *)btn
{
//    [UIView animateWithDuration:0.3f animations:^{
//        btn.transform = CGAffineTransformTranslate(btn.transform, 0, 50);
//    }];
    [UIView animateWithDuration:0.3f animations:^{
        btn.transform = CGAffineTransformTranslate(btn.transform, 0, 50);
    } completion:^(BOOL finished) {
//        if (finished) {
//            btn.transform = CGAffineTransformIdentity;
//        }
        if (finished) {
            [UIView animateWithDuration:0.3 animations:^{
                btn.transform = CGAffineTransformIdentity;
            }];
        }
    }];
    
   
}

- (void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
    [super dealloc];
}

@end

WXAlertView.h

typedef void(^WXBlock1)(int);

@interface WXAlertView : UIAlertView<UIAlertViewDelegate>

@property (nonatomic, copy)WXBlock1 alertBlock;

- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles;

WXAlertView.m

#import "WXAlertView.h"
#import <stdarg.h>

@implementation WXAlertView

//自定义的初始化方法
- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles
{
    self = [super initWithTitle:title
                        message:message
                       delegate:self
              cancelButtonTitle:cancelButtonTitle
              otherButtonTitles:otherButtonTitles, nil];
    if (self) {
        
    }
    return self;
}

//复写父类的初始化方法
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
    
    va_list ap;//定义一个va_list指针访问参数表
    id test;
    
    if (otherButtonTitles) {
        va_start(ap, otherButtonTitles);//初始化ap,让它指向第一个参数
        NSMutableArray *array = [[NSMutableArray alloc] init];
        while ((test = va_arg(ap, id))) {
            [array addObject:test];
            [self addButtonWithTitle:test];
        }
        NSLog(@"%@",array);
        
        va_end(ap);//清空参数列表,并且设置指针ap无效
    }
    
    
    
    return self;
    
}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (_alertBlock) {
        _alertBlock(buttonIndex);
    }
    
}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值