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的更多相关文章

随机推荐

  1. Eclipse 创建 Java 包

    打开新建 Java 包向导 你可以使用新建 Java 包向导来创建 Java 包.Java 包向导打开方式有: 通过点击 "File" 菜单并选择 New > Package ...

  2. python 爬虫4 cookies

    Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) 比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么 ...

  3. TBSchedule源码阅读2-TBScheduleManagerFactory 定时任务ManagerFactoryTimerTask

    定时任务 : 主要功能:监听zookeeper状态,正常则this.factory.refresh(),异常则this.factory.reStart(); 1 正常情况this.factory.re ...

  4. 误: Apache shutdown unexpectedly解决办法

    from:http://www.wopus.org/wordpress-basic/getting-started/2536.htmlXAMPP错  2015年10月20日15:58:19 新手上路发 ...

  5. 字符串匹配(KMP 算法 含代码)

    主要是针对字符串的匹配算法进行解说 有关字符串的基本知识 传统的串匹配法 模式匹配的一种改进算法KMP算法 网上一比較易懂的解说 小样例 1计算next 2计算nextval 代码 有关字符串的基本知 ...

  6. eclipse下设置tomcat,修改Java代码不必重启tomcat

    1.本文目的:用tomcat进行web开发时,修改Java代码往往要重启代码,当工程较大启动较慢时,严重影响效率,本文通过eclipse下tomcat开发和发布web程序时,对一些Java代码一般修改 ...

  7. 学习 Unix 常用命令

    第一个是 man 命令,作用是:"Display system documentation",我是 manual 的缩写.通过这个命令,我们能了解接下来要学习的命令的文档. ls, ...

  8. js的简单的逻辑算法题

    比如题目:寻找1~1000之内,所有能被5整除.或者能被6整除的数字 1 for(var i = 1 ; i <= 1000 ; i++){ 2  if(i % 5 == 0 || i % 6 ...

  9. 亚马逊订单api重构 api异常入库 在php中执行python

    https://docs.python.org/2/library/xml.etree.elementtree.html python  较 php能更高效第处理xml xpth php  扮演什么角 ...

  10. [转载]Apache在windows下的安装配置

    Apache在windows下的安装配置 转载自:http://blog.sina.com.cn/s/blog_536f16b00100cfat.html     1 Apache的下载 Apache ...