Block实战应用之button点击事件和Alert弹出框
来源:互联网 发布:工地临时用电计算软件 编辑:程序博客网 时间:2024/04/18 05:17
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
- Block实战应用之button点击事件和Alert弹出框
- 怎么用block给Button和AlertView添加点击事件
- 自动点击网页alert()弹出框
- iOS Block 处理UITableViewCell上button的点击事件
- iOS,OC给button添加block点击事件扩展
- 安卓实战中防止Button的频繁点击多次执行点击事件和点击切换文字
- 安卓实战中防止Button的频繁点击多次执行点击事件和点击切换文字
- 安卓实战中防止Button的频繁点击多次执行点击事件和点击切换文字
- iOS项目开发实战(Swift)—Button和Alert学习
- 点击button事件和拖动button事件冲突
- Button和Button事件的简单应用
- Andriod4.3之button相应点击事件
- Jquery点击按钮弹出alert
- ListView item点击事件和button点击事件并存
- ios7 单元格Cell点击事件和button点击事件冲突
- Button点击事件和LIstview子控件点击事件失效
- Button的监听事件和点击效果
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- Linux——线程信号量 读写锁 线程的私有数据
- 我的学生作业2
- SQL-有条件查询数据
- java批量修改Excel文件 使用包jxl.jar
- STM32 串口通讯 发送 接收
- Block实战应用之button点击事件和Alert弹出框
- 关于“非法使用精神控制技术和生理武器罪”的刑法增修案(正式建议稿)
- Android不刷机下的app2sd方法(dex cache占空间解决篇)
- linux vim,vi编辑器的基础
- OpenCV中的origin成员
- 【leetcode 数组】Remove Duplicates from Sorted Array II
- rac下/tmp/bootstrap权限问题
- 第九周项目3.4 输出点阵4
- java转义字符分类