AlertHandler.m 5.07 KB
//
//  AlertHandler.m
//  mkh_ios
//
//  Created by 钱鋆 on 15/6/30.
//  Copyright (c) 2015年 mkh.cn. All rights reserved.
//

#import "AlertHandler.h"
#import "MBProgressHUD.h"

@implementation AlertHandler

#pragma mark 创建单例
/**
 *  @author qianjun
 *
 *  @brief  创建单例
 *
 *  @return self
 */
+ (instancetype)sharedInstance {
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

#pragma mark -ios默认的alert
-(void)showAlert_default_title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle tag:(NSInteger)tag
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title
                                                       message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    alertView.tag = tag;
    alertView.opaque = YES;
    [alertView show];
}

- (void)f_showAlert_default_title:(NSString *)title message:(NSString *)message confirmHandler:(void (^)(UIAlertAction *action))confirmHandler confirmTitle:(NSString *)confirmTitle target:(UIViewController *)target {
    UIAlertController *_alertC_temp = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *_alertAct_confirm = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleCancel handler:confirmHandler];
    [_alertC_temp addAction:_alertAct_confirm];
    [target presentViewController:_alertC_temp animated:YES completion:nil];
}

- (void)f_showAlert_default_title:(NSString *)title message:(NSString *)message confirmHandler:(void (^)(UIAlertAction *action))confirmHandler confirmTitle:(NSString *)confirmTitle cancelHandler:(void (^)(UIAlertAction *action))cancelHandler cancelTitle:(NSString *)cancelTitle target:(UIViewController *)target {
    
    UIAlertController *_alertC_temp = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *_alertAct_confirm = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:confirmHandler];
    UIAlertAction *_alertAct_cancel = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
    
    [_alertC_temp addAction:_alertAct_confirm];
    [_alertC_temp addAction:_alertAct_cancel];
    [target presentViewController:_alertC_temp animated:YES completion:nil];
}

#pragma mark -ios默认的confirm
-(void)showConfirm_default_title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle tag:(NSInteger)tag
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title
                                                       message:message
                                                      delegate:delegate
                                             cancelButtonTitle:cancelButtonTitle
                                             otherButtonTitles:otherButtonTitle,nil];
    alertView.opaque = YES;
    alertView.tag = tag;
    
    [alertView show];
}

#pragma mark - 定义类似于toast显示效果的HUD(每次临时生成,效果结束后立即remove)
-(void)showToastHUD:(UIView *)addView message:(NSString *)message sleepTime:(int)sleepTime
{
    MBProgressHUD *hud_toast = [[MBProgressHUD alloc]initWithView:addView];
    [hud_toast setDetailsLabelFont:FONT_S_18];
    // 显示遮罩
    //hud_toast.dimBackground = YES;
    
    // 用detaillable,可以支持超长文字显示
    hud_toast.detailsLabelText = message;
    hud_toast.color = COLOR_S_LIGHTGRAY;
    
    hud_toast.mode = MBProgressHUDModeText;
    
    //[hud_toast setAlpha:1.0f];
    hud_toast.opaque = YES;
    
    [addView addSubview:hud_toast];
    
    [hud_toast show:YES];
    
    //指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示
    //hud_toast.yOffset = yOffset;
    //hud_toast.xOffset = xOffset;
    
    [hud_toast showAnimated:YES whileExecutingBlock:^{
        sleep(sleepTime);
    } completionBlock:^{
        [hud_toast removeFromSuperview];
    }];
}

#pragma mark - 自定义画面(每次临时生成,效果结束后立即remove)
-(void)showCustomHUD:(UIView *)addView Image:(NSString *)image message:(NSString *)message sleepTime:(int)sleepTime
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:addView animated:YES];
    hud.mode = MBProgressHUDModeCustomView;

//    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\U0000e8c3", 40, COLOR_C_mainColor)]];
// 
//    hud.customView = imgV;
    
    hud.square = YES;

    hud.labelText = message;
    [hud hide:YES afterDelay:sleepTime];
}

-(void)showDataSTempView:(UIView *)view sleepTime:(int)sleepTime
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    
    [hud hide:YES afterDelay:sleepTime];
}

@end