UIAlertController UIAlertView用法
2015-10-14 11:35
246 查看
项目中很多地方会出现弹出框框,来做个判断
基本方法如下
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
基本方法如下
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
相关文章推荐
- ios开发uitextfield怎么设置行数
- UIScrollView基本用法和代理方法
- dSYM-如何获取dSYM UUID并快速定位到已发布程序的crash位置(二)
- NameValueCollection 转换为 Json
- UISegmentControl
- msc和gcc编译器的内置函数(Compiler Intrinsics/Built-in Functions)
- UITableView详谈
- Gson用户指南(Gson User Guide)
- iOS开发UI篇—iPad和iPhone开发的比较
- 【学习ios之路:UI系列】ios中常见的几种控件.(UISlider,UISwitch,UIStepper,UISegmentedControl)
- UITextField小结
- UITableViewCell高度计算
- JIRA升级后原有Issue时间显示错乱的问题
- ALAssetsLibrary 与 UIImagePickerController区别
- 使用Ant编译项目,旧版SDK用apkbuilder打包,新版SDK没有apkbuilder工具,用sdklib.jar打包apk
- UITableViewCell 高度计算从混沌初始到天地交泰
- 为 ngui TweenPosition 添加 pingpongone
- easyui入门,ibaits where in
- UIImagePickerController选择图片发送后旋转90度的问题
- UIPageControl 圆点背景图片的自定义实现