OC UIAlertView简化使用

//
//  UIAlertView+Additions.h
//  V6
//
//  Created by aidong on 15/8/12.
//  Copyright (c) 2015年 aidong. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^UIAlertViewCallBackBlock)(NSInteger buttonIndex);

@interface UIAlertView (Additions)<UIAlertViewDelegate>

@property (nonatomic ,copy) UIAlertViewCallBackBlock alertViewCallBackBlock;

/**
 *  简单的AlertView,只有一句简单的提示信息。
 *
 *  @param message:提示信息
 *
 *  使用方法:[UIAlertView simpleAlert:@""];
 */
+ (void)simpleAlert:(NSString *)message;


/**
 *  一个带多个参数的AlertView
 *
 *  @param alertViewCallBackBlock:block(NSInteger buttonIndex)
 *  @param title:名称
 *  @param message:提示信息
 *
 *  使用方法:
 
     [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex){
     
     // 从左依次向右
     if (buttonIndex == 0) {
     
     NSLog(@"Press 0"); // 左边第一个button(cancelButton)
     
     }else if (buttonIndex == 1){
     NSLog(@"Press 1");
     }
     
     }title:@"提示" message:@"是否提交" cancelButtonName:@"NO" otherButtonTitles:@"YES"];
 *
 */
+ (void)alertWithCallBackBlock:(UIAlertViewCallBackBlock)alertViewCallBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonName:(NSString *)cancelButtonName otherButtonTitles:(NSString *)otherButtonTitles, ...;

@end


//
//  UIAlertView+Additions.m
//  V6
//
//  Created by aidong on 15/8/12.
//  Copyright (c) 2015年 aidong. All rights reserved.
//

#import "UIAlertView+Additions.h"
#import <objc/runtime.h>

static void *UIAlertViewKey = @"UIAlertViewKey";

@implementation UIAlertView (Additions)

+ (void)simpleAlert:(NSString *)message{
    
    UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

+ (void)alertWithCallBackBlock:(UIAlertViewCallBackBlock)alertViewCallBackBlock title:(NSString *)title message:(NSString *)message  cancelButtonName:(NSString *)cancelButtonName otherButtonTitles:(NSString *)otherButtonTitles, ... {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonName otherButtonTitles: otherButtonTitles, nil];
    NSString *other = nil;
    va_list args;
    if (otherButtonTitles) {
        va_start(args, otherButtonTitles);
        while ((other = va_arg(args, NSString*))) {
            [alert addButtonWithTitle:other];
        }
        va_end(args);
    }
    alert.delegate = alert;
    [alert show];
    alert.alertViewCallBackBlock = alertViewCallBackBlock;
}


- (void)setAlertViewCallBackBlock:(UIAlertViewCallBackBlock)alertViewCallBackBlock {
    
    [self willChangeValueForKey:@"callbackBlock"];
    /**
     1 创建关联(源对象,关键字,关联的对象和一个关联策略。)
     2 关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
     3 关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。
     */
    objc_setAssociatedObject(self, &UIAlertViewKey, alertViewCallBackBlock, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"callbackBlock"];
}

- (UIAlertViewCallBackBlock)alertViewCallBackBlock {
    
    return objc_getAssociatedObject(self, &UIAlertViewKey);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (self.alertViewCallBackBlock) {
        self.alertViewCallBackBlock(buttonIndex);
    }
}

/**
 OC中的关联就是在已有类的基础上添加对象参数。来扩展原有的类,需要引入#import <objc/runtime.h>头文件。关联是基于一个key来区分不同的关联。
 常用函数: objc_setAssociatedObject     设置关联
 objc_getAssociatedObject     获取关联
 objc_removeAssociatedObjects 移除关联
 */

@end

你可能感兴趣的:(ios,alert,oc,uialertview)