文章标题
2015-10-21 09:03
489 查看
自从iOS8 苹果推出了UIAlertController用来代替UIAlertView和UIActionSheet。也许大部分开发者并没有注意到。那时候UIAlertView和UIActionView都还可以使用。但是自从iOS9后,那两个就不废除了。现在统一用UIAlertController代替。 UIAlertView和UIActionSheet大家都很熟悉了。他的点击响应事件是通过协议代理来完成的。但是我们的UIAlertController的响应时间是闭包实现的,而不是委托代理实现的。下面简单的展示一下实现的代码。 1、实现UIAlertView的代码。 UIAlertController *alertControl = [UIAlertControlleralertControllerWithTitle:@"Alert"message:@"Alert text goes here"preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *noAction = [UIAlertActionactionWithTitle:@"No"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap No Button"); }]; UIAlertAction *yesAction = [UIAlertActionactionWithTitle:@"Yes"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap Yes Button"); }]; UIAlertAction *midleAction = [UIAlertActionactionWithTitle:@"Midle"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap Yes Midle"); }]; [alertControl addAction:noAction]; [alertControl addAction:yesAction]; [alertControl addAction:midleAction]; [selfpresentViewController:alertControlanimated:truecompletion:nil]; 2、实现UIActionSheet的代码。 UIAlertController *actionSheetControl = [[UIAlertControlleralloc]init]; UIAlertAction *cancalAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap取消 Button"); }]; UIAlertAction *destructiveAction = [UIAlertActionactionWithTitle:@"破坏性按钮"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap破坏性按钮 Button"); }]; UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"其他"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) { NSLog(@"Tap other Button"); }]; [actionSheetControladdAction:cancalAction]; [actionSheetControladdAction:destructiveAction]; [actionSheetControladdAction:otherAction]; [selfpresentViewController:actionSheetControlanimated:YEScompletion:nil]; 3、如果你的一个项目需要频繁的使用到警告框,每次都这么写有点繁琐,我们可以对其进行封装,你可以看到现在苹果是大力推荐使用block。所以我的封装也是使用block的。首先我们要创建一个类。同时倒入UIKit,否则不行的。详细的代码如下这是一个默认有两个按钮的警告框。我们需要传一个字典。字典中必须有title,message,oneAction,twoAction。
+(void)creatAlertView:(NSDictionary )contentDictory YesClick:(void (^)(void))yesClick NoClick:(void (^)(void))noClick alertView:(void (^) (UIAlertController alertView))alertView{
UIAlertController *alertControl = [UIAlertControlleralertControllerWithTitle:[contentDictoryobjectForKey:@”title”]message:[contentDictory objectForKey:@”message”]preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *noAction = [UIAlertActionactionWithTitle:[contentDictory objectForKey:@”oneAction”]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
yesClick();
}];
UIAlertAction *yesAction = [UIAlertActionactionWithTitle:[contentDictory objectForKey:@”twoAction”]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
noClick();
}];
[alertControl addAction:noAction];
[alertControl addAction:yesAction];
alertView(alertControl);
}
4、调用的方法是首先导入你创建的那个类。然后调用方法。具体的代码如下。
NSDictionary *contentDictory =@{@”title”:@”温馨提示”,@”message”:@”话费余额不足”,@”oneAction”:@”知道了”,@”twoAction”:@”去充话费”};
[AlertViewcreatAlertView:contentDictory YesClick:^{
NSLog(@”Yes”);
} NoClick:^{
NSLog(@”No”);
} alertView:^(UIAlertController *alertView) {
[selfpresentViewController:alertView animated:truecompletion:nil];
}];
5、当然UIAlertController也有别的新特性。其中最大的一个特性是文本的输入不受现在。并且可以自定义,不像以前的UIAlertView只可以输入两个文本框,并且只有一种样式。现在你只需要加一句代码就可以创建一个输入的文本框。代码是:
[alertControl addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder =@”话费余额”;
textField.text =@”账号:”;
NSLog(@”%@”,textField.text);
}];
相关文章推荐
- 自己动手写一个 iOS 网络请求库(五)——设置 SSL 钢钉
- 源码推荐(10.21):图片加载动画 渐变,一键启动动画
- 函数后面加一个const的作用
- C# 之 Word光标移动 GoTo 方法
- An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
- 路由器桥接(WIFI无线中继)设置及摆放位置图解
- 数据库虚拟补丁技术,打造轻安全
- qml常见问题及其解决
- Spring MVC提供的表单标签
- 关于Unity3d刚体撞击时的作用力的讨论贴
- 互联网+和创业潮,互联网+前提条件是什么?互联网+做什么?
- List之根据某个字段在add的时候过滤掉重复的数据
- 欢迎使用CSDN-markdown编辑器
- coolweather (二)
- 浅谈Java编程中的内存泄露情况
- TableViewCell 的CheckMark属性
- manifold learning 流形学习
- thinkphp四种url访问方式详解
- 抽象类
- w**RTK调试问题