用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. springboot-12-自定义拦截器的配置interceptor

    springmvc中拦截器的概念已经被弱化了, springboot中使用的也不甚广泛, 通常在用户登录等方面仍有用处 创建拦截器步骤: , 创建拦截器类继承HandlerInterceptor , ...

  2. boost::asio基本使用

    一.Asio网络库 截止到C++17,C++标准库都没有加入网络通信库.实际项目网络编程是非常常见的功能,直接使用操作系统API是低效率且不稳定的,比较好的方法是借助第三方成熟可靠的网络库.据我所知C ...

  3. springMVC中ModelAndView学写笔记

    api介绍: 构造函数摘要 ModelAndView()           bean样式用法的默认构造函数:填充bean属性,而不是传递构造函数参数. ModelAndView(Object vie ...

  4. 1000. A-B

    1000. A-B -----> http://soj.me/1000 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Descripti ...

  5. 记laravel5.5项目php-fpm迁移到swoole4.2.9

    事起说明 最近对上线半年多的laravel项目做了一次少大的改动,由php-fpm改为swoole,这里做个记录. 2019年过年前半个月,上阿里云后台查看前一天的访问请求日志,发现很多接口响应慢.翻 ...

  6. 浅谈angular2与angularJS的区别

    简介 大家好,今天给大家介绍一下angular,相信做过前端的小伙伴们都知道angular的大名,angularJS自2012年发布起就受到了大家的广泛关注.他首次提出了双向绑定概念让所有人都耳目一新 ...

  7. 微信小程序: 编译.wxss文件错误解决

    博主最近又重新开始捡起微信小程序,想做点自己的东西.了解到最近小程序工具有做更新,就顺手更新了最新的版本,功能比之前强大了不少!  更新归更新,更新后控制台就一直报下面这个错误:  解决办法 有问题总 ...

  8. [android] 手机卫士黑名单功能(短信拦截)

    前面我们把需要拦截的手机号都存储和展示出来了,接下来是使用广播接收者拦截短信了,这个广播接收者需要和一个服务绑定,服务开启的时候,接收者存在,服务停掉时,接收者关闭 在service包下定义一个类Ca ...

  9. Enable Scribble,Enable Guard Edges,Enable Guard Malloc,Zombie Objects

    最近项目中使用一个翻拍身份证信息识别活体检测的第三方框架,在使用时会偶然性的出现崩溃的现象,经过查找是因为第三方框架中有释放的内存区域再次引用引起的,因而补充一下相关知识点.   在Xcode Edi ...

  10. finally 的作用是什么?

    在java中finally首先必须使用在所有catch的最后位置, 无论是否抛出异常,finally代码块总是会被执行.就算是没有catch语句同时又抛出异常的情况下,finally代码块任然会被执行 ...