用block将UIAlertView与UIActionSheet统一起来
用block将UIAlertView与UIActionSheet统一起来
效果
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
注意
用block将UIAlertView与UIActionSheet统一起来的更多相关文章
- iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- iOS:简单使用UIAlertVIew和UIActionSheet
做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionS ...
- UIAlertView、 UIActionSheet
一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...
- iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet
UIAlertView/UIActionSheet UIAlertView //一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let on ...
- UIAlertView、UIActionSheet兼容iOS8
链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...
- UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案
本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertControlle ...
- iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?
iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...
- UIAlertControl的使用对比与UIAlertView和UIActionSheet
1.UIAlertVIew以-(void)show的方法显示: - (void)viewDidLoad { [super viewDidLoad]; //UIAlertView的使用 [self sh ...
- UIAlertView 与 UIActionSheet (提示用户)的使用方法
UIAlertView 提示用户 帮助用户选择框 // UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...
随机推荐
- AAAI2019 | 基于区域分解集成的目标检测 论文解读
Object Detection based on Region Decomposition and Assembly AAAI2019 | 基于区域分解集成的目标检测 论文解读 作者 | 文永亮 学 ...
- golang-利用反射给结构体赋值
由于想给一个结构体的部分成员赋值,但是有不知道具体名字,故将tag的json名字作为索引,按照json名字来一一赋值 1.通过tag反射//将结构体里的成员按照json名字来赋值 func SetSt ...
- InterView之PHP
PHP HTTP Keep-Alive的作用 作用 Keep-Alive:使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.Web服务器 ...
- 自动换行的两种代码(C#)
最近有个需求,需要将C# winform中的listBox中的内容自动换行, 其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题, 就是textBox中的文字会 ...
- httpClient 超时时间设置(转)
尊重博主原创,特贴博客链接.copy下来只怕以后链接失效或删掉. 转自:http://blog.csdn.net/hi_kevin/article/details/32316171 HttpClien ...
- 关于拓展jQuery功能插件的写法
有两种方法: 1.jQuery.fn.extend() 用于将一个对象的内容合并到jQuery的原型,以提供新的jQuery实例方法 例子: <span id="test"& ...
- 关于Win8快速启动失效解决
注册表修改HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 下BootExecute值为 autocheck空格 ...
- 【转】HttpServletRequestWrapper 实现xss注入
这里说下最近项目中我们的解决方案,主要用到commons-lang3-3.1.jar这个包的org.apache.commons.lang3.StringEscapeUtils.escapeHtml4 ...
- SpringBoot(三) Core Features: External Configuration(配置文件)
码云: external-configuration 可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置 一.属性值可以直接注入到bean 系统属性值不可以 // application ...
- java 将页面指定区域截图并上传到服务器
controller层: /** * 上传获取到的收据图片 * @param request * @param data 获取到的图片 * @return */ @RequestMapping(val ...