用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. openTSDB(转)

    1.OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is a distributed, scalable Time Series Datab ...

  2. C#开发一应用的总结

    要搭建测试环境. Webbrowser使用方面: 重新加载一页面后,要获取新的内容要使用重新使用browser.Document获取: HtmlElement的GetElementsByTagName ...

  3. [javaEE] http协议详细

    上一篇:http://www.cnblogs.com/taoshihan/p/5346731.html HTTP请求 请求行 GET /taoshihan/p/5346731.html HTTP/1. ...

  4. DOS窗口带jar包运行java程序

    由于工作环境的问题,有过一次这样的测试,需要在DOS窗口运行带有jar包的java程序 编译命令如下: javac -Djava.ext.dirs=./lib Test.java 或 javac -D ...

  5. Java 源程序与编译型运行区别

  6. 解决文字和text-decoration:underline下划线重叠问题

    一.text-decoration:underline下划线的问题 CSS text-decoration:underline可以给内联文本增加下划线,但是,如果对细节要求较高,就会发现,下划线经常会 ...

  7. 【1】Singleton模式(单例模式)

    一.单例模式的介绍 说到单例模式,大家第一反应应该就是--什么是单例模式?从“单例”字面意思上理解:一个类只有一个实例.所以单例模式也就是保证一个类只有一个实例的一种实现方法罢了(设计模式其实就是帮助 ...

  8. DDD Quickly - 读书笔记

    读后感:关于领域驱动设计,过去多多少少用到一些.所以,这本精简版看起来很快,很多概念很熟悉,它帮助我把散乱的知识串起来.最后,Eric Evans谈到一点,本来软件的发展是向着处理复杂的业务逻辑走的, ...

  9. jQuery源码学习笔记二

    //添加实例属性和方法 jQuery.fn = jQuery.prototype = { // 版本,使用方式:$().jquery弹出当前引入的jquery的版本 jquery: core_vers ...

  10. JavaScript--浅谈DOM操作

    JavaScript之浅谈DOM操作 1.理解DOM: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...