#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. SNF开发平台WinForm之九-代码生成器使用说明-SNF快速开发平台3.3-Spring.Net.Framework

    下面就具体的使用说明: 1.获取代码生成器的授权码(根据本机)-----还原数据库-------改config-----代码生成器 改代码生成器Config 2.登录代码生成器 3.查看是否连接成功 ...

  2. 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间

    代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; u ...

  3. RPM安装命令总结--转载

    原地址:http://www.cnblogs.com/zqwang0929/p/3352237.html 在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的 ...

  4. CentOS6.5菜鸟之旅:安装VirtualBox4.3

    一.下载VirtualBox的RHEL软件库配置文件 cd /etc/yum.repos.d wget http://download.virtualbox.org/virtualbox/rpm/rh ...

  5. DateTime to long

    private static DateTime BaseTime = new DateTime(1970, 1, 1); 将unixtime转换为.NET的DateTime public static ...

  6. 快速暴力解决Eclipse ADT和Android Studio兼容问题,创建同时兼容ADT和AS的安卓工程

    环境:AS 2.1.2+Java1.7+Gradle 2.14+ADT 24.0.2+MyEclipse 2015 前言:因为比赛要求使用ADT,而我本身比较习惯使用AS开发,遂想办法打造兼容两个ID ...

  7. [CLR via C#]19. 可空值类型

    我们知道,一个值类型的变量永远不可能为null.它总是包含值类型本身.遗憾的是,这在某些情况下会成为问题.例如,设计一个数据库时,可将一个列定义成为一个32位的整数,并映射到FCL的Int32数据类型 ...

  8. document.documentElement.clientWidth

    document.documentElement.clientWidth 摘自:http://blog.sina.com.cn/s/blog_6f1f9ead0100n1f6.html 关于获取各种浏 ...

  9. MySQL服务器权限表

    MySQL服务器通过权限表来控制用户对数据库的访问,权限表存放在mysql数据库里,由mysql_install_db脚本初始化.这些权限表分别user,db,table_priv,columns_p ...

  10. 购买SSL证书到部署网站遇到的若干问题

    作为一个菜鸟,对于SSL证书,我了解不多,只知道用了它网站更安全,所以这次使用SSL证书途中遇到了各方面的各种问题,到今天为止终于全部解决. 一.证书格式 前两天在那什么云上面买了个SSL证书,是Wo ...