UIAlertController弹出提示框
#import "RootViewController.h"
#import "RootView.h"
#define kColor arc4random() % 256 / 255.0 @interface RootViewController ()<UIAlertViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) RootView *rootView;
@property (nonatomic, strong) UIAlertController *colorAlert;
@property (nonatomic, strong) UIAlertController *warningAlert;
@end @implementation RootViewController - (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // 调用长按手势
[self longPressGesture];
} /**
* 添加长按手势UILongPressGestureRecognizer
*/
- (void)longPressGesture {
// 创建长按手势对象 -- UIAlertController
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)]; // 创建长按手势对象 -- UIAlertView
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 给myView添加长按手势
[self.rootView.myView addGestureRecognizer:longPress];
} /**
* 实现长按事件 -- UIAlertController
*
* @param longPress 长按手势
*/
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
// 手势开始时
if (longPress.state == UIGestureRecognizerStateBegan) {
// 创建UIAlertController对象
self.colorAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"改变颜色吗?" preferredStyle:UIAlertControllerStyleAlert]; // 创建UIAlertAction对象
// 确定按钮
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 实现给myView换随机背景颜色
self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:];
// 获取当前alert上所有的textField
NSArray *textFieldArr = self.colorAlert.textFields;
// 遍历获取到的textField数组,分别获取text
for (UITextField *field in textFieldArr) {
NSLog(@"text = %@", field.text);
}
}]; // 取消按钮
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 将action添加到alert上
[self.colorAlert addAction:action1];
[self.colorAlert addAction:action2]; // 添加TextField
// weak 弱引用(内存问题)
__weak RootViewController *rootVC = self;
[self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"用户名";
textField.delegate = rootVC; }];
[self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
// 密文输入
textField.secureTextEntry = YES;
textField.delegate = rootVC;
}]; // 模态推出
[self presentViewController:self.colorAlert animated:YES completion:nil]; }
} /**
* 实现textField代理方法,按return按钮回收键盘
*
* @param textField 当前textField
*
* @return
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
} /**
* 实现长按事件 -- UIAlertView
*
* @param longPress 长按手势
*/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
// 手势开始时
if (longPress.state == UIGestureRecognizerStateBegan) {
// 创建UIAlertView对象,并添加按钮
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"改变颜色吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"按钮1", nil];
/**
* 设置alertView样式
UIAlertViewStyleDefault = 0, -- 默认,没有输入框
UIAlertViewStyleSecureTextInput, -- 密文输入
UIAlertViewStylePlainTextInput, -- 明文输入
UIAlertViewStyleLoginAndPasswordInput -- 明文输入&密文输入结合,类似登录
*/
[alertView setAlertViewStyle:UIAlertViewStyleDefault];
// 显示alertView
[alertView show];
}
} /**
* 实现确定按钮点击事件
*
* @param alertView 当前alertView
* @param buttonIndex 按钮下标
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == ) { // index为按钮添加顺序,取消 -- 0 、确定 -- 1 、按钮1 -- 2
// 改变myView背景颜色
self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:];
} } /**
* 发生内存警告会自动调用
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
// 创建UIAlertController对象
self.warningAlert = [UIAlertController alertControllerWithTitle:@"内存问题" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 创建UIAlertAction对象,类型为警告
UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
// 将action添加到alert上
[self.warningAlert addAction:action]; // 模态推出alert
[self presentViewController:self.warningAlert animated:YES completion:nil]; } @end
UIAlertController弹出提示框的更多相关文章
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- PHP弹出提示框并跳转到新页面即重定向到新页面
本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下 这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...
- [转] 在Asp.net前台和后台弹出提示框
一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...
- SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框
1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...
- 基于Jquery 简单实用的弹出提示框
基于Jquery 简单实用的弹出提示框 引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间 ...
- iOS bug 之 H5 页面没有弹出提示框
描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...
- C#自动关闭弹出提示框
自动关闭弹出提示框(用一个小窗体显示提示信息):例如在一个form窗体中弹出自动关闭的提示框1.首先创建一个弹出提示信息的窗体 AutoCloseMassageBox,在里面拖一个lable控件,去掉 ...
- 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题
在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...
随机推荐
- MyBatis知多少(19)MyBatis操作
若要使用iBATIS执行的任何CRUD(创建,写入,更新和删除)操作,需要创建一个的POJO(普通Java对象)类对应的表.本课程介绍的对象,将“模式”的数据库表中的行. POJO类必须实现所有执行所 ...
- SharePoint 2013 搜索爬网功能
最近在政府部门介绍SharePoint 2013 新功能,我也准备了很多,比如SharePoint 2013的Search.以后有机会谈谈Office Web App,Workflow等. Share ...
- 《深入理解Java集合框架》系列文章
Introduction 关于C++标准模板库(Standard Template Library, STL)的书籍和资料有很多,关于Java集合框架(Java Collections Framewo ...
- const与readonly深度分析(.NET)
前言 很多.NET的初学者对const和readonly的使用很模糊,本文就const和readonly做一下深度分析,包括: 1. const数据类型的优势 2. const数据类型的劣势 3. r ...
- 连续值的CART(分类回归树)原理和实现
上一篇我们学习和实现了CART(分类回归树),不过主要是针对离散值的分类实现,下面我们来看下连续值的cart分类树如何实现 思考连续值和离散值的不同之处: 二分子树的时候不同:离散值需要求出最优的两个 ...
- Gradle学习系列之四——增量式构建
在本系列的上篇文章中,我们讲到了如何读懂Gradle的语法,在本篇文章中,我们将讲到增量式地构建项目. 请通过以下方式下载本系列文章的Github示例代码: git clone https://git ...
- 如何花样展示自己的摄影作品?CSS+JS+Html
注意:Windows平台推荐使用Edge.Chrome.FireFox,部分浏览器打不开 P.S.慢慢用鼠标在图片上拖拽会感觉更神奇 // 0.5 ? 1 : -1; }, ease: fun ...
- SQL年月日方面的查询信息
这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DAT ...
- C#属性封装
1.属性封装是为了保护与之相对应的字段的,保证字段的读取和赋值是否符合要求 2.属性可以分为:读写,只读,只写 3.允许外部访问的变量一定要申明为属性 4.属性的本质就是两个方法. 5.自动属性 6. ...
- VS2013 编译程序时提示 无法查找或打开 PDB 文件
"Draw.exe"(Win32): 已加载"C:\Users\YC\Documents\Visual Studio 2013\Projects\Draw\Debug\ ...