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 项目.打开向导的途径有: 通过点击 "File" 菜单然后选择 New > Java P ...

  2. CentOS 6.5 Ruby源码安装

    清除旧版Ruby,若存在 yum remove ruby 若为源码,使用如下命令 cd <your-ruby-source-path> make uninstall 下面开始安装Ruby ...

  3. python升级后pip 不可用 卸载pip

    python版本由2.6升级到2.7之后,用pip提示报错 找了一下原因,网上的版本很多.弄来弄去比较麻烦 来点简单粗暴的 1.卸载pip yum remove python-pip 2.下载 cur ...

  4. firefox(火狐)怎么关闭鼠标拖拽搜索

    工具-附加组件-卸载<附加组件管理器> 即可. 这玩意真心坑爹,起这个名字的人绝对是吃屎了,这名字怎么和鼠标拖拽混到一起的 !!!   关键字:火狐:鼠标:鼠标拖拽:鼠标手势:关闭

  5. Laravel Lumen 数组操作

    php原生:http://www.w3school.com.cn/php/php_ref_array.asp Lumen方法:https://laravel.com/docs/5.6/helpers ...

  6. 转:: 刺鸟:用python来开发webgame服务端(4)

    来源:http://ciniao.me/article.php?id=14 --------------- 刺鸟原创文章,转载请注明出处    前面的工作都已准备就绪,现在我们得来看看服务端怎么和客户 ...

  7. CodeIgniter框架——CI中视图路径问题

    答: 视图中的所有路径全部和 index.php 同级,也就是和 index.php 属于一个目录下,也就是网站根目录. 因为 index.php 后面看似是路径的东西其实那只是一种 URL 参数而已 ...

  8. windows下 兼容Python2和Python3

    windows下同时安装了python2和python3时,都可以配置环境变量,如果在命令行里输入python命令,windows会去环境变量里寻找Python的安装位置,如果先找到pytoon2的, ...

  9. python学习【第十一篇】网络编程

    一.socket的简介 socket(简称:套接字)进程间通信的一种方式,它与其他进程间通信的一个主要不同是:能实现不同主机间的进程间通信,我们网络上各种各样的服务大多都是基于 Socket 来完成通 ...

  10. [Spring Data MongoDB]学习笔记--_id和类型映射

    _id字段的映射: MongoDB要求所有的document都要有一个_id的字段. 如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId. { , "accounts&qu ...