UIAlertViewController详解
来源:互联网 发布:小孩多大开始学编程 编辑:程序博客网 时间:2024/05/22 03:30
Xcode7更新后,苹果不建议使用以前的AlertView和ActionSheet了,而是将二者合一,创建了一个新的视图控制器UIAlertViewController,UIAlertViewController继承自UIViewController,通过改变设置不同的UIAlertControllerStyle来确定显示的是AlertView或者是ActionSheet.通过添加AlertAction来增加按钮,话不多说,贴上自己写的一个小Demo.
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self CreateUI];}- (void)CreateUI{ UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setFrame:CGRectMake(100, 100, 100, 50)]; [button1 setTitle:@"ActionSheet" forState:UIControlStateNormal]; button1.backgroundColor = [UIColor blackColor]; [button1 addTarget:self action:@selector(ShowActionSheet) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1]; UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button2 setFrame:CGRectMake(100, 180, 100, 50)]; [button2 setTitle:@"AlertView" forState:UIControlStateNormal]; button2.backgroundColor = [UIColor blackColor]; [button2 addTarget:self action:@selector(ShowAlertView) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button2]; }- (void)prepareAlertCWithStyle:(UIAlertControllerStyle)alertstyle{ UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@"这是AlertController的测试" message:@"iOS8以后用UIAlertController" preferredStyle:alertstyle]; // 添加取消按钮UIAlertActionStyleCancel UIAlertAction *canleaction = [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { NSLog(@"点击了取消按钮"); }]; [_alertC addAction:canleaction]; // 添加普通按钮UIAlertActionStyleDefault UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"普通按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"点击了普通按钮"); }]; [_alertC addAction:defaultaction]; // 添加文本框,只能在alertview模式下添加textfiled if (alertstyle == UIAlertControllerStyleAlert) { [_alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) { // 这里可以对文本框进行一些自定义操作 textField.placeholder = @"请输入....."; }]; } // 添加UIAlertActionStyleDestructive样式按钮 UIAlertAction *replaceaction = [UIAlertAction actionWithTitle:@"销毁按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { if (alertstyle == UIAlertControllerStyleAlert) { NSLog(@"%@",_alertC.textFields[0].text); } NSLog(@"点击了销毁样式按钮"); }]; [_alertC addAction:replaceaction]; // 显示alertview,UIAlertController是继承与UIViewController的 [self presentViewController:_alertC animated:YES completion:nil];}- (void)ShowActionSheet{ // 设置为ActionSheet样式 [self prepareAlertCWithStyle:UIAlertControllerStyleActionSheet];}- (void)ShowAlertView{ // 设置为AlertView样式 [self prepareAlertCWithStyle:UIAlertControllerStyleAlert];}
2 0
- UIAlertViewController详解
- UIAlertViewController
- UIAlertViewController
- UIAlertViewController
- iOS8- UIAlertViewController
- iOS8- UIAlertViewController
- KVO 和 UIAlertViewController
- 使用UIAlertviewController弹出视图
- iOS UIALertViewController常用方法
- UIAlertViewController的使用方法
- iOS 9 的 UIAlertViewController
- iOS8之后的UIAlertViewController
- Swift 提示框 UIAlertViewController
- 自定义UIAlertViewController选择视图
- 在iOS8中使用UIAlertViewController
- iOS之UIAlertViewController精简使用
- iOS8新特性UIAlertViewController,UISearchController,UIPopoverPresentationController,WKWebView
- 提示框的综合使用--UIAlertViewController
- 【杭电oj】1999-不可摸数(打表)
- 走出阴影,走向新生活
- iOS Core Animation 简明系列教程
- postgres不错的命令
- Android入门心得(一)
- UIAlertViewController详解
- [数据存储之一]NSCoding协议练习
- 读书学习之道
- postgresql分区表1
- 黑马程序员--Java编程之集合阶段总结
- 第13周项目2 算法验证—Dijkstra算法
- PHP对数组的常用处理
- HDU 5273 Dylans loves sequence
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法