因为block比较好用,你懂得,直接上代码:
.h
typedef void (^choiceCompletionBlock)(int index);
+ (instancetype)shareAlertView;
#pragma mark - 弹窗
//普通提示弹窗
- (void)showTipAlert:(NSString *)message;
- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion;
//可以选择的弹窗
- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion;
- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion;
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion;
// 可自定义Title
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion;
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion;
//三选择弹窗
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion;
.m
choiceCompletionBlock _choiceCompletion;
- (void)showTipAlert:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion
{
if (_choiceCompletion) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
_choiceCompletion = [completion copy];
}
- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion
{
[self showChoiceAlert:message doneTitle:@"确定" completion:completion];
}
- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion
{
[self showChoiceAlert:message title:TIPTITLE doneTitle:doneTitile completion:completion];
}
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion {
if (_choiceCompletion) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:doneTitle, nil];
[alert show];
_choiceCompletion = [completion copy];
}
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion
{
if (_choiceCompletion) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, nil];
[alert show];
_choiceCompletion = [completion copy];
}
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion {
if (_choiceCompletion) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:button1Title, button2Title, nil];
[alert show];
_choiceCompletion = [completion copy];
}
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion
{
if (_choiceCompletion) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, title3, nil];
[alert show];
_choiceCompletion = [completion copy];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (_choiceCompletion) {
_choiceCompletion(buttonIndex);
_choiceCompletion = nil;
}
}
上面的代码会有个问题,就是假如代码多次调用alert的时候就只能显示一个了,解决的办法我想到了一个不错的。
#import <objc/runtime.h>
#define EOCMyAlertViewKey @"EOCMyAlertViewKey"
- (void)showTipAlert:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
- (void)showTipAlert:(NSString *)message completion:(choiceCompletionBlock)completion
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);
[alert show];
}
- (void)showChoiceAlert:(NSString *)message completion:(choiceCompletionBlock)completion
{
[self showChoiceAlert:message doneTitle:@"确定" completion:completion];
}
- (void)showChoiceAlert:(NSString *)message doneTitle:(NSString *)doneTitile completion:(choiceCompletionBlock)completion
{
[self showChoiceAlert:message title:TIPTITLE doneTitle:doneTitile completion:completion];
}
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title doneTitle:(NSString *)doneTitle completion:(choiceCompletionBlock)completion {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:doneTitle, nil];
objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);
[alert show];
}
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 completion:(choiceCompletionBlock)completion
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, nil];
objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);
[alert show];
}
- (void)showChoiceAlert:(NSString *)message title:(NSString *)title button1Title:(NSString *)button1Title button2Title:(NSString *)button2Title completion:(choiceCompletionBlock)completion {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:button1Title, button2Title, nil];
objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);
[alert show];
}
- (void)showChoiceAlert:(NSString *)message button1Title:(NSString *)title1 button2Title:(NSString *)title2 button3Title:(NSString *)title3 completion:(choiceCompletionBlock)completion
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, title3, nil];
objc_setAssociatedObject(alert, EOCMyAlertViewKey, completion, OBJC_ASSOCIATION_COPY);
[alert show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
choiceCompletionBlock block = objc_getAssociatedObject(alertView, EOCMyAlertViewKey);
if (block) {
block(buttonIndex);
}
}
使用runtime中的关联就能很好的解决问题,也不用数组什么的!