AlertView 和 UIAlertController 简析学习和简单示例
2015-10-17 12:24
465 查看
AlertView:就是在iOS 中作为弹窗的一个控件, 例如, 登陆成功/失败 弹框提示, 输入有误弹框提示等.
UIAlertController : 是 iOS 8 出现的弹窗的控件, 功能更加强大,使用方便与否就看各位看官习惯而定,在此不做评论, 官方推荐使用这个控件作为弹窗使用, 下面也将用一个例子介绍以及简单使用
首先介绍 UIAlertView 新建一个工程 然后….
下面使用一个简单里例子来介绍UIAlertController 控件
简单介绍到此结束, 多谢各位看官捧场…
【本文由“0奋斗途中0”发布,2017年5月10日】
UIAlertController : 是 iOS 8 出现的弹窗的控件, 功能更加强大,使用方便与否就看各位看官习惯而定,在此不做评论, 官方推荐使用这个控件作为弹窗使用, 下面也将用一个例子介绍以及简单使用
首先介绍 UIAlertView 新建一个工程 然后….
// 遵守AlertView 协议 @interface ViewController ()<UIAlertViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 以点击登陆 弹窗登陆成功为例 // 创建一个button UIButton * loginButton = [UIButton buttonWithType:(UIButtonTypeSystem)]; // 标识button的大小 loginButton.frame = CGRectMake(100, 100, 100, 100); // 设置背景颜色 loginButton.backgroundColor = [UIColor orangeColor]; // 添加button 显示文字 [loginButton setTitle:@"登陆" forState:(UIControlStateNormal)]; // 添加点击事件 并设置触发方式 [loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:(UIControlEventTouchUpInside)]; // 添加button到页面中 [self.view addSubview:loginButton]; } // 实现 loginButton 点击方法 - (void)loginButtonAction { #pragma mark -- 核心代码 -- 只需要这两行代码即可运行 -- 添加button方法 目地是演示一下基础用法 // 第一种弹框方式 UIAlertView // 自定义弹框 UIAlertView 以登陆成功为例 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]; // 添加AlertView 一定要有这一行 [alertView show]; } // 实现代理方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { // 判断是 确定 或者 取消 按钮 if (buttonIndex == 0) { // 由于电脑计数从0 开始 则第一个按钮数字为0 NSLog(@"点击确定按钮, 为第%ld个按钮", buttonIndex); } else { // 由于只有两个选项 另一个按钮数字为1 也就是取消按钮 NSLog(@"点击取消按钮, 为第%ld个按钮", buttonIndex); } }
下面使用一个简单里例子来介绍UIAlertController 控件
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置背景颜色 为白色 self.view.backgroundColor = [UIColor whiteColor]; // 以点击登陆 弹窗登陆成功为例 // 创建一个button UIButton * loginButton = [UIButton buttonWithType:(UIButtonTypeSystem)]; // 标识button的大小 loginButton.frame = CGRectMake(100, 100, 100, 100); // 设置背景颜色 loginButton.backgroundColor = [UIColor orangeColor]; // 添加button 显示文字 [loginButton setTitle:@"祖国祝福语" forState:(UIControlStateNormal)]; // 添加点击事件 并设置触发方式 [loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:(UIControlEventTouchUpInside)]; // 添加button到页面中 [self.view addSubview:loginButton]; } // 实现 loginButton 点击方法 - (void)loginButtonAction { #pragma mark -- 核心代码 -- 由于使用controller 形式,所以使用方式和老的AlertView 不同 -- // 第二种方式 UIAlertController // 自定义 UIAlertController UIAlertController * alertAC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请写下您的祝福语" preferredStyle:(UIAlertControllerStyleAlert)]; // 创建确定按钮 // handler: 处理点击事件 用 block 回调 UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { // 处理点击事件 // 取出添加的文本框 UITextField * textField = alertAC.textFields.firstObject; NSLog(@"%@", textField.text); // 取出按钮 UIAlertAction * textAction = alertAC.actions.firstObject; NSLog(@"%@", textAction); }]; // 将按钮添加到 alertAC 上 [alertAC addAction:sureAction]; // 创建取消按钮 UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) { // 处理点击事件 // 取出点击按钮 并输出在控制台上 UIAlertAction * textAction = alertAC.actions.lastObject; NSLog(@"%@", textAction); }]; // 将按钮添加到 alertVC 上 [alertAC addAction:cancelAction]; // 还可以添加通知信息文本框 [alertAC addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"爱我大中华!"; textField.textColor = [UIColor redColor]; }]; // 将按钮 加载屏幕上 相当于 alertView 中的 show [self presentViewController:alertAC animated:YES completion:nil]; }
简单介绍到此结束, 多谢各位看官捧场…
【本文由“0奋斗途中0”发布,2017年5月10日】
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android控件之CheckBox、RadioButton用法实例分析
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- WinForm实现移除控件某个事件的方法