UIAlertController弹出提示框

  1 #import "RootViewController.h"
  2 #import "RootView.h"
  3 #define kColor arc4random() % 256 / 255.0
  4
  5 @interface RootViewController ()<UIAlertViewDelegate, UITextFieldDelegate>
  6 @property (nonatomic, strong) RootView *rootView;
  7 @property (nonatomic, strong) UIAlertController *colorAlert;
  8 @property (nonatomic, strong) UIAlertController *warningAlert;
  9 @end
 10
 11 @implementation RootViewController
 12
 13 - (void)loadView {
 14     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 15     self.view = self.rootView;
 16 }
 17
 18 - (void)viewDidLoad {
 19     [super viewDidLoad];
 20     // Do any additional setup after loading the view.
 21
 22     // 调用长按手势
 23     [self longPressGesture];
 24 }
 25
 26 /**
 27  *  添加长按手势UILongPressGestureRecognizer
 28  */
 29 - (void)longPressGesture {
 30     // 创建长按手势对象 -- UIAlertController
 31         UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
 32
 33     // 创建长按手势对象 -- UIAlertView
 34 //    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
 35     // 给myView添加长按手势
 36     [self.rootView.myView addGestureRecognizer:longPress];
 37 }
 38
 39
 40 /**
 41  *  实现长按事件 -- UIAlertController
 42  *
 43  *  @param longPress 长按手势
 44  */
 45 - (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
 46     // 手势开始时
 47     if (longPress.state == UIGestureRecognizerStateBegan) {
 48         // 创建UIAlertController对象
 49         self.colorAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"改变颜色吗?" preferredStyle:UIAlertControllerStyleAlert];
 50
 51
 52         // 创建UIAlertAction对象
 53         // 确定按钮
 54         UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
 55             // 实现给myView换随机背景颜色
 56             self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:1];
 57                         // 获取当前alert上所有的textField
 58                         NSArray *textFieldArr = self.colorAlert.textFields;
 59                         // 遍历获取到的textField数组,分别获取text
 60                         for (UITextField *field in textFieldArr) {
 61                             NSLog(@"text = %@", field.text);
 62                         }
 63         }];
 64
 65
 66         // 取消按钮
 67         UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
 68         // 将action添加到alert上
 69         [self.colorAlert addAction:action1];
 70         [self.colorAlert addAction:action2];
 71
 72
 73         // 添加TextField
 74         // weak 弱引用(内存问题)
 75         __weak RootViewController *rootVC = self;
 76                 [self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 77                     textField.placeholder = @"用户名";
 78                     textField.delegate = rootVC;
 79
 80                 }];
 81                 [self.colorAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 82                     textField.placeholder = @"密码";
 83                     // 密文输入
 84                     textField.secureTextEntry = YES;
 85                     textField.delegate = rootVC;
 86                 }];
 87
 88
 89         // 模态推出
 90         [self presentViewController:self.colorAlert animated:YES completion:nil];
 91
 92     }
 93 }
 94
 95
 96 /**
 97  *  实现textField代理方法,按return按钮回收键盘
 98  *
 99  *  @param textField 当前textField
100  *
101  *  @return
102  */
103 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
104     [textField resignFirstResponder];
105     return YES;
106 }
107
108
109 /**
110  *  实现长按事件 -- UIAlertView
111  *
112  *  @param longPress 长按手势
113  */
114 - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
115     // 手势开始时
116     if (longPress.state == UIGestureRecognizerStateBegan) {
117         // 创建UIAlertView对象,并添加按钮
118         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"改变颜色吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"按钮1", nil];
119         /**
120          *  设置alertView样式
121          UIAlertViewStyleDefault = 0,  -- 默认,没有输入框
122          UIAlertViewStyleSecureTextInput,  -- 密文输入
123          UIAlertViewStylePlainTextInput,   -- 明文输入
124          UIAlertViewStyleLoginAndPasswordInput -- 明文输入&密文输入结合,类似登录
125          */
126         [alertView setAlertViewStyle:UIAlertViewStyleDefault];
127         // 显示alertView
128         [alertView show];
129     }
130 }
131
132 /**
133  *  实现确定按钮点击事件
134  *
135  *  @param alertView   当前alertView
136  *  @param buttonIndex 按钮下标
137  */
138 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
139     if (buttonIndex == 1) { // index为按钮添加顺序,取消 -- 0 、确定 -- 1 、按钮1 -- 2
140         // 改变myView背景颜色
141         self.rootView.myView.backgroundColor = [UIColor colorWithRed:kColor green:kColor blue:kColor alpha:1];
142     }
143
144 }
145
146
147
148 /**
149  *  发生内存警告会自动调用
150  */
151 - (void)didReceiveMemoryWarning {
152     [super didReceiveMemoryWarning];
153     // Dispose of any resources that can be recreated.
154     // 创建UIAlertController对象
155     self.warningAlert = [UIAlertController alertControllerWithTitle:@"内存问题" message:nil preferredStyle:UIAlertControllerStyleAlert];
156     // 创建UIAlertAction对象,类型为警告
157     UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
158     // 将action添加到alert上
159     [self.warningAlert addAction:action];
160
161     // 模态推出alert
162     [self presentViewController:self.warningAlert animated:YES completion:nil];
163
164 }
165
166 @end
时间: 2024-08-10 17:44:25

UIAlertController弹出提示框的相关文章

jQuery - 选中复选框则弹出提示框

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>判断是否选中</title> <script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script> <script

如何实现android蓝牙开发 自动配对连接,并不弹出提示框

如何实现android蓝牙开发 自动配对连接,并不弹出提示框 之前做一个android版的蓝牙,遇到最大的难题就是自动配对. 上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了 我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我. 在源码 BluetoothDevice 类中还有两个隐藏方法 cancelBondProcess()和cancelPairingUserInput() 这两个方法一个是取消配对进

asp.net 后台代码跳转页面前弹出提示框

1.Response.Write("<script>alert('查询语句执行出错!');window.location.href=DisplayData.aspx</script>"); 2.Page.RegisterStartupScript("msg", "<script>alert('查询语句执行出错!');window.location.href='DisplayData.aspx'</script>

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

引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间自己写了个弹出效果,放到项目上去发现效果还不错,这里贴出来,做个备忘,有需要的同学可以拿去,也可以作为一个参考. 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.d

经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法

经验总结:WebBrowser自动点击弹出提示框alert.弹出对话框confirm.屏蔽弹出框.屏蔽弹出脚本错误的解决办法 网上有好多解决方法,可是不一定好使,本人经过多次试验,针对WebBrowser控件中自动点击弹出框及禁用脚本提示问题得到如下几种实际情况的解决办法,绝对管用. 1.屏蔽弹出错误脚本 将WebBrowser控件ScriptErrorsSuppressed设置为True即可. (参考本篇博客:http://www.cnblogs.com/qqflying/archive/20

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

一.在前台弹出提示框 1.点击“A”标记或者“控件按钮”弹出提示框 <asp:LinkButton ID="lbtnDel" runat="server" OnClientClick='<%# "if(!confirm("你确定退订吗?"))return false;"%>' Text="删除"/> 2.方法二: <asp:LinkButton ID="lbtnDel

鼠标悬浮在超链接上弹出提示框

鼠标悬浮在超链接上弹出提示框:大家知道超链接有一个title属性,当鼠标放在链接的时候,可以出现一个提示框效果,不过自带的效果虽然廉价但往往并不物美,所以需要自定义一个,下面是一个纯CSS实现的这样的效果,和大家分享一下.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="h

js弹出提示框并跳转页面

1.提示框有两个选择项,点确定跳转,取消停留在原页面ScriptManager.RegisterStartupScript(Page, this.GetType(), "", "<script>if(confirm('请登录?')){location.href='login.aspx'};</script>", false); 2.提示框只有一个确定按钮,跳转到指定页面ScriptManager.RegisterStartupScript(p

android中常用的弹出提示框

转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等方式,重写我们自己的对话框.当然,这也是不失为一个不错的解决方式,但是一般的情况却是这样,我们重写的对话框,也许只在一个特定的地方会用到,为了这一次的使用,而去创建一个新类,往往有