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的更多相关文章
随机推荐
- Linq源代码阅读
在 System.Core dotnet451\source\ndp\fx\src\core\system\linq\enumerable.cs Where 和 Select 内 ,把数组和List分 ...
- JavaScript核心(晋级高手必读篇)
本文是对“ECMA-262-3 in detail”系列学习内容的概述与总结.如果你对ES3系列文章感兴趣,本文每一节内容均包含相应ES3系列章节的链接,以供阅读与获取更深入的解释. 本文预期读者:有 ...
- django使用redis做缓存
Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...
- CentOS系统bash: groupadd: command not found问题
如果我们需要在CentOS执行新建用户组命令的时候,需要进入到ROOT权限,如果你用以下命令: 1 su2 su root 进入到ROOT账户,那么会出现上述的错误信息:“bash: groupadd ...
- boost::lockfree::spsc_queue
#include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...
- EasyPlayer-RTSP-Android安卓播放器播放RTSP延迟优化策略,极低延时!
EasyPlayer-RTSP-Android安卓RTSP播放器低延迟播放延时优化策略 EasyPlayer-RTSP-Android播放器是一款专门针对RTSP协议进行过优化的流媒体播放器,其中我们 ...
- C# new和override区别(转)
override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...
- 利用jdk中工具完成Java程序监控方法记录
转载加自己整理的部分内容,转载自:http://jiajun.iteye.com/blog/810150 记录下JConsole使用方法 一.JConsole是什么 从Java 5开始 引入了 ...
- Python基础-set集合
1.集合的创建 s = set('fansik and fanjinbao') print(s) 打印结果(去掉了重复的字符):{'k', 'd', 'f', 'n', ' ', 'j', 'i', ...
- python学习之路-第一天-接触python
我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...