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