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的更多相关文章
随机推荐
- genymotion启动报错
启动genymotion时提示 网络配置有问题,经检查是网卡virtualBox配置的问题,把ip设为自动获取即可 重新启动就可以了
- Linux & windows 应用服务器&Oracle数据库服务器备份
Linux篇 tomcat启动加入开机启动项 vi /etc/rc.d/rc.local service iptables stop /usr/local/tomcat/bgyappserv01/ ...
- retrival and clustering: week 2 knn & LSH 笔记
华盛顿大学 <机器学习> 笔记. knn k-nearest-neighbors : k近邻法 给定一个 数据集,对于查询的实例,在数据集中找到与这个实例最邻近的k个实例,然后再根据k个最 ...
- 杂项收集,包括-发邮件、二维码生成、文件下载、压缩、导出excel
本篇就最近工作解决的问题做个代码收集.包括以下几个方面:发邮件.二维码生成.文件下载.压缩.导出excel.有一种可用的解决方法就好,不求全面,不求原理. 1.发邮件: 命名空间:System.Net ...
- 浅析Avicii的MV Hey Brother
date: 2017-04-10 20:30:19 其实这篇随感早应在一个月之前就写完的,但是笔者刚从老家来到北京,需要安顿各种事情,再加上自己比较懒上班比较忙,每天晚上回到家,都是直接趴在床上睡了. ...
- VMware下安装centos6.7的步骤
一.虚拟机的创建 1.点击创建新的虚拟机(图片红框的位置) 2.选择自定义安装.下一步(根据自己的需要有选择的进行选择) 3.新装的虚拟机的版本和虚拟机兼容的产品以及这个版本的虚拟机的限制(这个窗口没 ...
- 【BZOJ5055】膜法师 树状数组
[BZOJ5055]膜法师 Description 题目描述 在给定的维度序列a中, 求出所有满足i<j<k且ai<aj<ak的ai*aj*ak的和 即 ∑ (a_i*a_j* ...
- 《从零开始学Swift》学习笔记(Day 31)——存储属性
原创文章,欢迎转载.转载请注明:关东升的博客 Swift中的属性分为存储属性和计算属性,存储属性就是Objective-C中的数据成员,计算属性不存储数据,但可以通过计算其他属性返回数据. 存储属性可 ...
- iOS 关于自定义UICollectionViewLayout实现复杂布局
UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...
- JavaScript函数setInterval()和setTimeout()正确的写法
一.常规写法 1.1 不传参数 function a (x, y) { var i = 0; var b = function(){ console.log((x * y) + (i++)); } r ...