用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

注意

时间: 2024-10-12 14:35:05

用block将UIAlertView与UIActionSheet统一起来的相关文章

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

1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. //UIAlertView和UI

iOS:简单使用UIAlertVIew和UIActionSheet

做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionSheet可以弹出一个选择列表,让用户选择列表中的某一项操作.使用UIAlertVIew和UIActionSheet非常简单,以下是一个简单的示例代码: //UIAlertView - (void)someButtonClicked {//初始化AlertView UIAlertView *alert

在iOS8.0之后的UIAlertView和UIActionSheet的新写法

在iOS8.0之后,苹果更新了UIAlertView和UIActionSheet的创建方法.在以前的版本中,这两个提醒用户的控件各自有自己的创建方法,但是在iOS8.0之后,使用了UIAlertController这个控制器类统一创建.不过之前的创建方法并没有在iOS8.0之后的版本中实效,依然可以正常使用.下边就记录下新的写法. 首先看苹果API的示例写法: UIAlertController* alert = [UIAlertController alertControllerWithTit

UIAlertView和UIActionSheet

##UIAlertView和UIActionSheet ```objc// cancelButton buttonIndex == 1// destructiveButton buttonIndex ==0 UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"确定注销吗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@&q

iOS开发——UI篇Swift篇&amp;UIAlertView/UIActionSheet

UIAlertView/UIActionSheet UIAlertView 1 //一个按钮的提醒 2 @IBAction func oneButtonAler() 3 { 4 //创建单一按钮提醒视图 5 let oneButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定")

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

本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertController代替: 1.Alert用法 UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"This is Title" message:@"This is message" prefer

iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的漂亮人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 弃用了! 如今得用 UIAlertController 了. 使用两个样式来相应这两个控件: UIAlertView - UIAlertContr

IOS学习第二课 UIAlertView和UIActionSheet

1    UIAlertView 类似于Android中的Dialog,简单用法如下: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Messate" delegate:nil cancelButtonTitle:@"Cancle" otherButtonTitles:nil, nil]; [alertView show]; 2   U

IOS UIAlertView 和 UIActionSheet的区别

UIAlertView 和 UIActionSheet的区别: 1.弹框位置不同: UIAlertView弹框显示在中间 UIActionSheet弹框显示在底端 2.是否可以实现文本框的输入(参考:http://www.ithao123.cn/content-9409772.html) UIAlertView可以实现,而UIActionSheet不可以实现. #import "RootViewController.h" @interface RootViewController ()