ios实战-使用Block的UIAlertView

因为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中的关联就能很好的解决问题,也不用数组什么的!

转载于:https://my.oschina.net/iq19900204/blog/402933

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值