OC UIAlertView简化使用

本文介绍了在Objective-C(OC)中如何简化使用UIAlertView,主要针对iOS应用开发,通过实例展示了创建和显示警告对话框的简便方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值