UIAlertController UIAlertView用法

项目中很多地方会出现弹出框框,来做个判断

基本方法如下

    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否确定退出程序" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelA = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

     

    }];

   UIAlertAction *configA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

     

         //执行的代码

        }];

        

        

    }];

    

    [alertC addAction:cancelA];

    [alertC addAction:configA];

    

    [self presentViewController:alertC animated:YES completion:nil];

 

因为很多地方都要用到,每次写太麻烦了 ,简单封装一下成为一个工具,提高,,,

 1. 创建一个TOOl继承于NSObject

@interface UIAlertTool : NSObject

   - (void)initshowAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancel:(NSString        *)cancelButtonTitle other:(NSString *)otherButtonTitle confirmHandle:(void (^)())confirm cancelHandle:(void (^)())cancle;

 

@end

  2.实现部分

 

#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

#import "UIAlertTool.h"

 

typedef void (^confirm)();

typedef void (^cancle)();

 

@interface UIAlertTool()

{

    confirm confirmParam;

    cancle cancleParam;

}

@end

@implementation UIAlertTool

- (void)initshowAlertView:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message cancel:(NSString *)cancelButtonTitle other:(NSString *)otherButtonTitle confirmHandle:(void (^)())confirm cancelHandle:(void (^)())cancle

{ confirmParam = confirm;

    cancleParam = cancle;

    if (IAIOS8) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

            

            cancle();

        }];

        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            

            confirm();

        }];

        [alertController addAction:cancelAction];

        [alertController addAction:otherAction];

        

        [viewController presentViewController:alertController animated:YES completion:nil];

      

    } else{

        UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil];

        [TitleAlert show];

    }

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==0) {

        confirmParam();

    } else{

        cancleParam();

    }

}

@end

 

转载于:https://www.cnblogs.com/fengkuangIT/p/4877001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值