博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS_UIAlertController
阅读量:7190 次
发布时间:2019-06-29

本文共 4609 字,大约阅读时间需要 15 分钟。

 

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 
@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==0)    {        confirmParam();    }    else{        cancleParam();    }}@end

 

转载于:https://www.cnblogs.com/xjf125/p/4853499.html

你可能感兴趣的文章
随机抽样一致性算法(RANSAC)
查看>>
[LeetCode] Repeated Substring Pattern 重复子字符串模式
查看>>
Linux下makefile(一)
查看>>
OAF 使用 javascript 使某个按钮在5秒内不能重复点击
查看>>
编程语言的可移植性
查看>>
ArcGIS Spatial Query
查看>>
Axel替代wget
查看>>
JNI学习积累之三 ---- 操作JNI函数以及复杂对象传递
查看>>
firefox怎么修改tls协议号
查看>>
C# 中的"yield"使用
查看>>
vue-todolist
查看>>
【大型网站技术实践】初级篇:借助LVS+Keepalived实现负载均衡
查看>>
更新项目经常使用的Linux命令
查看>>
tracert路由跟踪工具使用方法
查看>>
OGG学习笔记03-单向复制简单故障处理
查看>>
软件性能指标
查看>>
第4章 Selenium2-java WebDriver API (三)
查看>>
Coding.net+Myeclipse 2014 Git配置
查看>>
PHP-问题处理Fatal error: Uncaught Error: Call to undefined function mb_strlen()
查看>>
学习 OpenStack 的方法论 - 每天5分钟玩转 OpenStack(150)
查看>>