用block将UIAlertView与UIActionSheet统一起来
2015-06-24 09:35
507 查看
效果
1. 将代理方法的实例对象方法转换成了类方法使用
2. 要注意单例block不要长期持有,用完就释放掉
源码
https://github.com/YouXianMing/UIInfomationView
注意
1. 将代理方法的实例对象方法转换成了类方法使用
2. 要注意单例block不要长期持有,用完就释放掉
源码
https://github.com/YouXianMing/UIInfomationView
// // UIInfomationView.h // Alert // // Created by YouXianMing on 15/6/23. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void (^ClickAtIndexBlock)(NSInteger buttonIndex); @interface UIInfomationView : NSObject /** * 弹出AlertView对话框 * * @param title 标题 * @param message 信息 * @param cancelButtonTitle 取消按钮 * @param otherButtons 其他按钮 * @param clickAtIndex 获取点击信息的block(进入block中的对象请用weak修饰,否则会导致被block持有) * * @return AlertView对象 */ + (UIAlertView *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex; /** * 弹出ActionSheet对话框 * * @param view 要显示的view * @param title 标题 * @param cancelButtonTitle 取消按钮 * @param destructiveButton destructive按钮 * @param otherButtons 其他按钮 * @param clickAtIndex 获取点击信息的block(进入block中的对象请用weak修饰,否则会导致被block持有) * * @return ActionSheet对象 */ + (UIActionSheet *)showActionSheetInView:(UIView *)view WithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButton otherButtonTitles:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex; @end
// // UIInfomationView.m // Alert // // Created by YouXianMing on 15/6/23. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "UIInfomationView.h" #import <UIKit/UIKit.h> /** * 让类方法中的对象被持有 */ static ClickAtIndexBlock _clickAtIndexBlock; @interface UIInfomationView () <UIActionSheetDelegate, UIAlertViewDelegate> @end @implementation UIInfomationView + (UIAlertView *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex { _clickAtIndexBlock = [clickAtIndex copy]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil]; for(NSString *buttonTitle in otherButtons) { [alert addButtonWithTitle:buttonTitle]; } [alert show]; return alert; } + (UIActionSheet *)showActionSheetInView:(UIView *)view WithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButton otherButtonTitles:(NSArray *)otherButtons clickAtIndex:(ClickAtIndexBlock)clickAtIndex { _clickAtIndexBlock = [clickAtIndex copy]; UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title delegate:[self self] cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButton otherButtonTitles:nil]; for(NSString *buttonTitle in otherButtons) { [sheet addButtonWithTitle:buttonTitle]; } [sheet showInView:view]; return sheet; } #pragma mark - alertView代理 + (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { _clickAtIndexBlock(buttonIndex); } + (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger) buttonIndex { _clickAtIndexBlock = nil; } #pragma mark - actionSheetView代理 + (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { _clickAtIndexBlock(buttonIndex); } + (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { _clickAtIndexBlock = nil; } @end
注意
相关文章推荐
- Android 使用正则表达式来快速生成多套dpi及分分辨率适配的UI
- UIControl
- 10 DelayQueue 延时队列类——Live555源码阅读(一)基本组件类
- UIButton 设置圆角 边框颜色 点击回调方法 &&图片在右文字在左
- 9 DelayQueueEntry 延时队列节点类——Live555源码阅读(一)基本组件类
- 【to be continue】五金配件,省得以后不认识
- iOS UI Label控件
- MSBuild 常用命令(Copy,Zip)
- UE4项目学习——2D跑酷游戏尝试(蓝图脚本)1
- Flash Builder找不到所需的调试器版本
- 多线程之dispatch_queue_t
- zkoss框架异常:org.zkoss.zk.ui.metainfo.PropertyNotFoundException: Method setLable not found for class or
- iOS UI 视图
- MsBuild 入门
- HDU 1423(Greatest Common Increasing Subsequence)公共最长不降子序列
- 自定义UIPageControl圆点的图片
- 自定义UIPageControl
- U9单据UI开发--单据类型UI开发
- QT国际化 一 (lupdate/linguits/lrelease)
- iOS 9: UIStackView入门