码迷,mamicode.com
首页 > 其他好文 > 详细

UIAlertController弹出提示框

时间:2016-04-17 17:52:15      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

  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

 

UIAlertController弹出提示框

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5401475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!