#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. Linux高级编程--05.文件读写

    缓冲I/O和非缓冲I/O 文件读写主要牵涉到了如下五个操作:打开.关闭.读.写.定位.在Linux系统中,提供了两套API, 一套是C标准API:fopen.fclose.fread.fwrite.f ...

  2. Scene视图辅助线绘制

    有时候需要在Scene视图中绘制一些辅助线,方便进行一些编辑的工作,可以通过如下类和函数完成: 绘制辅助线,相关类: Gizmos类:用于在Scene视图中绘制调试信息或辅助线,这些辅助线只有在Sce ...

  3. 利用__index和__newindex实现默认值表、监控表、只读表

    __index和__newindex实际上相当于是在读写表的时候分别加了一道过滤的逻辑,让读写表的操作可以被监控或说回调,利用这个特性可以实现一些带有特殊功能的表. 带有默认值的表: setdefau ...

  4. c++中基类与派生类中隐含的this指针的分析

    先不要看结果,看一下你是否真正了解了this指针? #include<iostream> using namespace std; class Parent{ public: int x; ...

  5. ruby -- 基础学习(五)empty、nil、blank三者之间的区别

    这三个方法在ROR中经常用到,都是用来判断是否为空的. 区别是: ruby的方法:.nil?..empty? rails的方法 :.blank? 用法的区别: .nil?    :   判断对象是否存 ...

  6. Gulp:新一代前端构建利器

    1.什么是Gulp gulp.js 是一种基于流的,代码优于配置的新一代构建工具. Gulp 和 Grunt 类似.但相比于 Grunt 的频繁的 IO 操作,Gulp 的流操作,能更快地完成构建. ...

  7. Android 学习笔记之AndBase框架学习(一) 实现多功能标题栏

    PS:Volley框架终于通过看源码的方式完成了所有的学习..开始学习AndBase...AndBase的源码实在是多的离谱...因此就不对所有的源码进行分析了... 学习内容: 1.使用AndBas ...

  8. 转载:混淆包含SlidingMenu、gson等Android代码的proguard写法

    描述:作为一个写了几年c#的码农,咱的java水平实在不敢恭维,但是最近有个任务需要弄点java的代码,还要研究混淆.网上百度并实验了整整一天,终于算是有点成果了.其中混淆的时候由于用到了gson包, ...

  9. Django--上传文件

    需求 Django中forms表单上传文件处理​ 速查 views.py 1 2 3 4 5 6 7 8 9 def upload(request):     if request.method==' ...

  10. 深度使用react-native的热更新能力,必须知道的一个shell命令

    开篇之前,先讲一个自己开发中的一个小插曲: 今天周日,iOS版 App 周一提交,周三审核通过上架,很给力.不过,中午11:30的时候,运营就反应某个页面有一个很明显的问题,页面没法拉到底部,部分信息 ...