iOS_UIAlertController
ViewController.m文件 #import "ViewController.h"
@interface ViewController ()
//
@property (nonatomic, strong) UIAlertController *alert;
@property (nonatomic, strong) UIAlertController *actionSheet;
@property (nonatomic, weak) IBOutlet UIButton *showAlertBtn;
@property (nonatomic, weak) IBOutlet UIButton *showActionSheetBtn;
@end @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建UIAlertController
self.alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
//动作
UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消");
}];
//动作
UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了确定");
}];
//动作
UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了销毁");
}];
//将所有动作添加到控制器中
[self.alert addAction:act1];
[self.alert addAction:act2];
[self.alert addAction:act3]; self.actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet];
// 在action sheet中,UIAlertActionStyleCancel不起作用
UIAlertAction *act4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了取消");
}];
UIAlertAction *act5 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了确定");
}];
UIAlertAction *act6 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了销毁");
}];
[self.actionSheet addAction:act4];
[self.actionSheet addAction:act5];
[self.actionSheet addAction:act6];
}
//显示单击事件
- (IBAction)showAlert:(id)sender {
[self presentViewController:self.alert animated:YES completion:^{ }];
}
- (IBAction)showActionSheet:(id)sender { [self presentViewController:self.actionSheet animated:YES completion:^{ }];
}
@end
运行效果图:
iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.
UIAlertTool.h文件 #import <Foundation/Foundation.h> @interface UIAlertTool : NSObject
-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;
;@end
UIAlertTool.m文件 #import "UIAlertTool.h" #define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)();
typedef void (^cancle)();
@interface UIAlertTool()
{
confirm confirmParam;
canclecancleParam;}
@end @implementation UIAlertTool
-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle
{
confirmParam=confirm;
cancleParam=cancle;
if (IAIOS8)
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
{
cancle();
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
confirm();
}];
// Add the actions.
[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==)
{
confirmParam();
}
else{
cancleParam();
}
}
@end
iOS_UIAlertController的更多相关文章
随机推荐
- dm8148 开发之---IDR帧
IDR帧属于I帧. 解码器收到IDR帧时,将所有的参考帧队列丢弃(用x264_reference_reset函 数实现——在encoder.c文件中).这点是所有I帧共有的特性,但是收到IDR帧 时, ...
- 很easy的js双向绑定框架(二):控制器继承
初衷 上一篇已经实现了数据的双向绑定,但model的控制范围是整个文档.在实际project中必需要有作用范围,以便做ui模块的拆分. 这一篇,我们希望实现像angularjs一样的控制器继承: 1. ...
- echarts Y轴刻度保留几位小数
yAxis: [ { type: 'value', name: '雨量(mm)', nameLocation: 'start', inverse: true, axisLabel: { ...
- libevent在windows下用visual studio编译时出现error C2894错误的原因与解决方法
libevent是一个使用很广泛的网络库,今天想了解下它.于是去git clone了一份源码,用vs2005的命令行:nmake -f makefile.nmake编译之,顺利编译通过,生成三个静态库 ...
- python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)
二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准 ...
- 3、ACE-实用生活口语---讲打电话Talking on the phone
(3)需要留口信吗?请让他给我回电话Would you like to leave a message?May I take a message for you ?Please tell him to ...
- (分享)Linux服务器如何防止中木马
大家的windows机器可能经常装一些杀毒软件或者什么的来防止中毒,然而在Linux上我们应该怎么防止这些呢? 在面试过程中我们也经常遇到该类问题,那么我们应该怎么回答才显得既有逻辑又有深度呢? 首先 ...
- python学习【第九篇】python面向对象编程
一.面向对象了解 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. Pyth ...
- FFF at Valentine(强连通分量缩点+拓扑排序)
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- On the fly test
on the fly test就是边开发边测试的意思.test code不是早就生成好的,在一边生成code的同时一边做test running,最大的好处是,flexiable ,每一次可以选择不同 ...