#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弹出提示框的更多相关文章

  1. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  2. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  3. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...

  4. [转] 在Asp.net前台和后台弹出提示框

    一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...

  5. SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框

    1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...

  6. 基于Jquery 简单实用的弹出提示框

    基于Jquery 简单实用的弹出提示框 引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间 ...

  7. iOS bug 之 H5 页面没有弹出提示框

    描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...

  8. C#自动关闭弹出提示框

    自动关闭弹出提示框(用一个小窗体显示提示信息):例如在一个form窗体中弹出自动关闭的提示框1.首先创建一个弹出提示信息的窗体 AutoCloseMassageBox,在里面拖一个lable控件,去掉 ...

  9. 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题

    在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...

随机推荐

  1. Android加载SO库UnsatisfiedLinkError错误的原因及解决方案

    Android 应用开发者应该对 UnsatisfiedLinkError 这种类型的错误比较熟悉了,这个问题一直困扰着广大的开发者,那么有没有想过有可能你什么都没做错,也会出现这个问题呢? 我们在 ...

  2. jquery 按钮效果 正常、移上、按下

    在网页设计过程中,经常遇见按钮的各状态效果.写了一个jquery扩展,使这个过程更方便! 使用前注意引用Jquery: JqueryExtend.js: (function ($) { // Butt ...

  3. list2json

    list2json方法 System.Web.Script.Serialization.JavaScriptSerializer serial = new System.Web.Script.Seri ...

  4. Supervisor – 用于 Unix 系统的进程监控工具

    Supervisor 是一个客户端/服务端模式的系统,使用户能够监视和控制 UNIX 操作系统的进程.Supervisor 为你提供一个地方来启动,停止和监视进程.进程可以单独或成组的形式控制.您还可 ...

  5. vertical-align两种应用场合

    vertical-align两种应用场合 (1)用在td/th中或display:table-cell元素中:让当前元素中的文本内容在竖直方向上居中    css部分:    .content{   ...

  6. 谈Mysql索引

    myisam和innodb的索引有什么区别? 两个索引都是B+树索引,但是myisam的表存储和索引存储是分开的,索引存储中存放的是表的地址.而innodb表存储本身就是一个B+树,它是用主键来做B+ ...

  7. HT图形组件设计之道(三)

    上篇我们通过定制了CPU和内存展示界面,体验了HT for Web通过定义矢量实现图形绘制与业务数据的代码解耦及绑定联动,这类案例后续文章还会继续以便大家掌握更多的矢量应用场景,本篇我们先切换个话题, ...

  8. IOS开发中设置控件内容对齐方式时容易混淆的几个属性

    IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NST ...

  9. 《深入理解Java集合框架》系列文章

    Introduction 关于C++标准模板库(Standard Template Library, STL)的书籍和资料有很多,关于Java集合框架(Java Collections Framewo ...

  10. 【第三课】ANR和OOM——贪快和贪多的后果(下)

    Out of Mana,法力耗尽. 内存就像法力,耗尽了就什么都不能做了.有时候一个应用程序占用了太大的内存,超过了Android系统为你规定的限制,那么系统就会干掉你,以保证其他app有足够的内存. ...