Files
ASHUIAlertView/ASHUIAlertView.m
T
2015-06-29 13:51:21 +08:00

95 lines
2.8 KiB
Objective-C

//
// ASHUIAlertView.m
// Ash_AWord
//
// Created by xmfish on 15/6/29.
// Copyright (c) 2015年 ash. All rights reserved.
//
#import "ASHUIAlertView.h"
#import <objc/runtime.h>
@interface ASHUIAlertView ()<UIAlertViewDelegate>
@end
@implementation ASHUIAlertView
+ (id)showAlertWithTitle:(NSString *)title message:(NSString *)message
completionBlock:(void (^)(NSUInteger buttonIndex, ASHUIAlertView *alertView))block
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
if (!cancelButtonTitle && !otherButtonTitles) {
return nil;
}
ASHUIAlertView *alertView = [[self alloc] initWithTitle:title
message:message
completionBlock:block
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
if (otherButtonTitles != nil) {
id eachObject;
va_list argumentList;
if (otherButtonTitles) {
[alertView addButtonWithTitle:otherButtonTitles];
va_start(argumentList, otherButtonTitles);
while ((eachObject = va_arg(argumentList, id))) {
[alertView addButtonWithTitle:eachObject];
}
va_end(argumentList);
}
}
[alertView show];
return alertView;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
void (^block)(NSUInteger buttonIndex, UIAlertView *alertView) = objc_getAssociatedObject(self, "blockCallback");
if (block) {
block(buttonIndex, self);
}
}
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
completionBlock:(void (^)(NSUInteger buttonIndex, ASHUIAlertView *alertView))block
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
objc_setAssociatedObject(self, "blockCallback", [block copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (self = [self initWithTitle:title
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil]) {
if (cancelButtonTitle) {
[self addButtonWithTitle:cancelButtonTitle];
self.cancelButtonIndex = [self numberOfButtons] - 1;
}
id eachObject;
va_list argumentList;
if (otherButtonTitles) {
[self addButtonWithTitle:otherButtonTitles];
va_start(argumentList, otherButtonTitles);
while ((eachObject = va_arg(argumentList, id))) {
[self addButtonWithTitle:eachObject];
}
va_end(argumentList);
}
}
return self;
}
@end