用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统一起来的更多相关文章

  1. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  2. iOS:简单使用UIAlertVIew和UIActionSheet

    做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionS ...

  3. UIAlertView、 UIActionSheet

    一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...

  4. iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet

    UIAlertView/UIActionSheet UIAlertView //一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let on ...

  5. UIAlertView、UIActionSheet兼容iOS8

    链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...

  6. UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案

    本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertControlle ...

  7. iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

    iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  8. UIAlertControl的使用对比与UIAlertView和UIActionSheet

    1.UIAlertVIew以-(void)show的方法显示: - (void)viewDidLoad { [super viewDidLoad]; //UIAlertView的使用 [self sh ...

  9. UIAlertView 与 UIActionSheet (提示用户)的使用方法

    UIAlertView 提示用户  帮助用户选择框 //    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...

随机推荐

  1. Nodejs学习笔记(十二)—定时任务(node-schedule)

    写在之前 在实际开发项目中,会遇到很多定时任务的工作.比如:定时导出某些数据.定时发送消息或邮件给用户.定时备份什么类型的文件等等 一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非 ...

  2. log4配置

    log4j 和 log4j2 方式一:log4j2.xml 添加 jar 包 <!-- log4j-core --> <!-- <dependency> <grou ...

  3. JavaScript窗口打开与关闭及如何使用opener使子窗口对父窗口进行操作

    一.打开与关闭窗口 1.打开窗口:可以使用window对象中的Open()方法. newWindow = window.open(url,windowname,location); 参数说明: url ...

  4. Shell如何解决文件流管道的文本拼接失效问题

    前言: 近期由于业务的需要,需实现通过监控日志文件的内容并定时将日志的有效内容通过邮件进行告警. 文本内容的格式如下: 1 aaa 2 bbb 4 ccc 7 ddd 希望输出: bbb ccc 版本 ...

  5. Java Date SimpleDateFormat

    public static void main(String[] args) { long millis = 1492741275301L; Calendar calendar = Calendar. ...

  6. Deep Q-Network 学习笔记(四)—— 改进②:double dqn

    这篇没搞懂...这里只对实现做记录. 修改的地方也只是在上一篇的基础上,在“记忆回放”函数里,计算 target Q 时取值做下调整即可. def experience_replay(self): & ...

  7. C# 中的委托和事件 --转载

    作者:张子阳 转载源:  http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx C# 中的委托 ...

  8. Oracle时间换算:日,月,周数,星期,年

    http://blog.csdn.net/liangweiwei130/article/details/37930383 Oracle时间换算,留做记号!

  9. java 获取控制台输入

    读取控制台输入 从控制台读取一行数据,返回值字符串 public class IO { public static void main(String args[]) throws IOExceptio ...

  10. spring 中 InitializingBean 接口使用理解

    前言:这两天在看 spring 与 quart 的集成,所以了解到 spring 是如何初始化 org.springframework.scheduling.quartz.SchedulerFacto ...