IOS UIAlertView和UIAlertController学习记录
2016-01-19 14:16
351 查看
介绍
UIAlertView 是ios9.0版本以前用来做弹出式警告对话框的。 UIAlertController 是ios9.0版本以后,苹果推荐使用的用来实现警告对话框的。并且它将UIAlertView和UIActionSheet结合在一起了。
UIAlertView 常用的流程
1.初始化一个UIAlertView的对象,并指定用于显示的标题、内容、需要哪些按钮信息、指定监听的代理对象等。
//参数otherButtonTitles是可以不止输入一个实参的,以便实现多按钮的弹出对话框 UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"通知" message:@"恭喜你通关" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重玩", nil];
2.显示对话框。
[view show];
3.在步骤1中我们指定了监听这个警告对话框的代理为当前类对象,我当前在ViewController中,故我需要在我当前类的声明中,遵守UIAlertViewDelegate协议,并实现一些你需要实现的方法。这里我们需要实现的方法一般有,判断哪个按钮按下了。
//遵守协议 @interface ViewController : UIViewController <UIAlertViewDelegate> ...... //按钮点击触发方法,buttonIndex的顺序是按照步骤1中,按钮指定的顺序来的。如:Cancel按钮Index为0 -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%ld",buttonIndex); }
UIAlertController的常用方法
1.声明并初始化一个UIAlertController对象。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"通知" message:@"恭喜你通关" preferredStyle:UIAlertControllerStyleAlert];
2.创建UIAlertAction。
//创建alertAction,并绑定按钮,用block的方法指定触发方法执行代码块 [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确定 被按下了"); //这里是按钮点击后,处理方法运行处 }]]; //创建 ”重玩“ alertAction按钮 [alertController addAction:[UIAlertAction actionWithTitle:@"重玩" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"重玩 被按下了"); self.index = -1; [self nextGroup]; }]]; //若想创建textfiled,可以这样做 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { NSLog(@"添加一个textField就会调用 这个block"); }];
3.显示出来。
[self presentViewController:alertController animated:YES completion:nil];
使用UIAlertController省去了设置代理并实现协议方法的步骤,对按钮的点击事件都写在block回调中,更加明了和简单。并且将UIAlertView 和 UIActionSheet 两个合二为一了,更方便了,更方便管理和扩展。
相关文章推荐
- 红警conquer.mix文件
- Android Volley完全解析(三),定制自己的Request
- LeetCode_Implement Queue using Stacks_队列操作
- 95. Unique Binary Search Trees II
- 加载图片的三种方法imageNamed、imageWithContentsOfFile、NSData
- UE4 4.9 - cast modulated shadows causes flickering on IOS dynamic characters
- C# wpf WebBrowser遮挡上层UI的解决办法
- Jenkins+Git 集成测试(build、zip、curl)
- How to Change RabbitMQ Queue Parameters in Production?
- UE3模块化阴影
- UE4 Directional Lights
- UE4 Dynamic Scene Shadows
- Request的getParameter和getAttribute方法的区别
- Request的getParameter和getAttribute方法的区别
- LabWindows/CVI入门之第二章:GUI开发
- Divide and conquer:Moo University - Financial Aid(POJ 2010)
- UIImageView
- 在iOS开发中关于(UIDevice、NSBundle、NSLocale)注意:凡是使用这些控件必须引入“UIKit.h”框架
- UISearchBar becomeFirstResponder不生效解决
- iOS 【UIKit-Quartz2D使用(绘制基本图形)】