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