1. ViewController.m文件
  2.  
  3. #import "ViewController.h"
  4. @interface ViewController ()
  5. //
  6. @property (nonatomic, strong) UIAlertController *alert;
  7. @property (nonatomic, strong) UIAlertController *actionSheet;
  8. @property (nonatomic, weak) IBOutlet UIButton *showAlertBtn;
  9. @property (nonatomic, weak) IBOutlet UIButton *showActionSheetBtn;
  10. @end
  11.  
  12. @implementation ViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. //创建UIAlertController
  16. self.alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
  17. //动作
  18. UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  19. NSLog(@"点击了取消");
  20. }];
  21. //动作
  22. UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  23. NSLog(@"点击了确定");
  24. }];
  25. //动作
  26. UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  27. NSLog(@"点击了销毁");
  28. }];
  29. //将所有动作添加到控制器中
  30. [self.alert addAction:act1];
  31. [self.alert addAction:act2];
  32. [self.alert addAction:act3];
  33.  
  34. self.actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet];
  35. // 在action sheet中,UIAlertActionStyleCancel不起作用
  36. UIAlertAction *act4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  37. NSLog(@"点击了取消");
  38. }];
  39. UIAlertAction *act5 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  40. NSLog(@"点击了确定");
  41. }];
  42. UIAlertAction *act6 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  43. NSLog(@"点击了销毁");
  44. }];
  45. [self.actionSheet addAction:act4];
  46. [self.actionSheet addAction:act5];
  47. [self.actionSheet addAction:act6];
  48. }
  49. //显示单击事件
  50. - (IBAction)showAlert:(id)sender {
  51. [self presentViewController:self.alert animated:YES completion:^{
  52.  
  53. }];
  54. }
  55. - (IBAction)showActionSheet:(id)sender {
  56.  
  57. [self presentViewController:self.actionSheet animated:YES completion:^{
  58.  
  59. }];
  60. }
  61. @end

运行效果图:

iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.

  1. UIAlertTool.h文件
  2.  
  3. #import <Foundation/Foundation.h>
  4.  
  5. @interface UIAlertTool : NSObject
  6. -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;
  7. ;@end
  1. UIAlertTool.m文件
  2.  
  3. #import "UIAlertTool.h"
  4.  
  5. #define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)();
  6. typedef void (^cancle)();
  7. @interface UIAlertTool()
  8. {
  9. confirm confirmParam;
  10. canclecancleParam;}
  11. @end
  12.  
  13. @implementation UIAlertTool
  14. -(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle
  15. {
  16. confirmParam=confirm;
  17. cancleParam=cancle;
  18. if (IAIOS8)
  19. {
  20. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  21. // Create the actions.
  22. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
  23. {
  24. cancle();
  25. }];
  26. UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
  27. {
  28. confirm();
  29. }];
  30. // Add the actions.
  31. [alertController addAction:cancelAction];
  32. [alertController addAction:otherAction];
  33. [viewController presentViewController:alertController animated:YES completion:nil];
  34. }
  35. else
  36. {
  37. UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil];
  38. [TitleAlert show];
  39. }
  40. }
  41. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  42. {
  43. if (buttonIndex==)
  44. {
  45. confirmParam();
  46. }
  47. else{
  48. cancleParam();
  49. }
  50. }
  51. @end

iOS_UIAlertController的更多相关文章

随机推荐

  1. Linq源代码阅读

    在 System.Core dotnet451\source\ndp\fx\src\core\system\linq\enumerable.cs Where 和 Select 内 ,把数组和List分 ...

  2. JavaScript核心(晋级高手必读篇)

    本文是对“ECMA-262-3 in detail”系列学习内容的概述与总结.如果你对ES3系列文章感兴趣,本文每一节内容均包含相应ES3系列章节的链接,以供阅读与获取更深入的解释. 本文预期读者:有 ...

  3. django使用redis做缓存

    Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...

  4. CentOS系统bash: groupadd: command not found问题

    如果我们需要在CentOS执行新建用户组命令的时候,需要进入到ROOT权限,如果你用以下命令: 1 su2 su root 进入到ROOT账户,那么会出现上述的错误信息:“bash: groupadd ...

  5. boost::lockfree::spsc_queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...

  6. EasyPlayer-RTSP-Android安卓播放器播放RTSP延迟优化策略,极低延时!

    EasyPlayer-RTSP-Android安卓RTSP播放器低延迟播放延时优化策略 EasyPlayer-RTSP-Android播放器是一款专门针对RTSP协议进行过优化的流媒体播放器,其中我们 ...

  7. C# new和override区别(转)

    override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...

  8. 利用jdk中工具完成Java程序监控方法记录

    转载加自己整理的部分内容,转载自:http://jiajun.iteye.com/blog/810150 记录下JConsole使用方法 一.JConsole是什么    从Java 5开始 引入了 ...

  9. Python基础-set集合

    1.集合的创建 s = set('fansik and fanjinbao') print(s) 打印结果(去掉了重复的字符):{'k', 'd', 'f', 'n', ' ', 'j', 'i', ...

  10. python学习之路-第一天-接触python

    我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...