IOS UIAlertView 警报视图
最编程
2024-03-13 21:52:52
...
3月16日,北京源创会 —— “数据库,2024 开炫”
一 UIAlertView 简介
如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView。
二 UIAlertView 创建
/**
1.创建 UIAlertView
title 提示视图标题,比如 告警、提示、异常
message 用户看的实际讯息
delegate 可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
cancelButtonTitle 可选参数,这个字符串符会显示在提示示视图的取消按钮上。
otherButtonTitles 可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
*/
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:nil
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
三 设置样式
/**
2. 设置样式
UIAlertViewStyleDefault = 0, 默认,没有输入框
UIAlertViewStyleSecureTextInput, 提示视图中添加密码框
UIAlertViewStylePlainTextInput, 提示视图中添加输入框
UIAlertViewStyleLoginAndPasswordInput 登录和密码框
*/
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
四 展示
[alertView show];
五 监听点击,并获取用户的输入
如果要监听用户的点击和获取用户输入,需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中
/**
* 监听点击
*
* @param alertView <#alertView description#>
* @param buttonIndex <#buttonIndex description#>
*/
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
// 判断点击
if ([buttonTitle isEqualToString:@"Cancel"]){
NSLog(@"User pressed the Cancel button.");
}
else if ([buttonTitle isEqualToString:@"Ok"]){
NSLog(@"User pressed the Ok button.");
}
//接受输入类容
//textFieldAtIndex 获取对应位置的UITextField
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(@"%@",textField.text);
UITextField *textField2 = [alertView textFieldAtIndex:1];
NSLog(@"%@",textField2.text);
}
六 完整代码
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UIAlertView 作用
//如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView
/**
1.创建 UIAlertView
title 提示视图标题,比如 告警、提示、异常
message 用户看的实际讯息
delegate 可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
cancelButtonTitle 可选参数,这个字符串符会显示在提示示视图的取消按钮上。
otherButtonTitles 可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
*/
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:nil
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
/**
2. 设置样式
UIAlertViewStyleDefault = 0, 默认,没有输入框
UIAlertViewStyleSecureTextInput, 提示视图中添加密码框
UIAlertViewStylePlainTextInput, 提示视图中添加输入框
UIAlertViewStyleLoginAndPasswordInput 登录和密码框
*/
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
/**
3. 监听点击
如果要监听用户的点击需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中
*/
[alertView setDelegate:self];
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
// message:@"Message"
// delegate:self
// cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
/**
4. 接受AlertView 输入类容
*/
//展示
[alertView show];
}
/**
* 监听点击
*
* @param alertView <#alertView description#>
* @param buttonIndex <#buttonIndex description#>
*/
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
// 判断点击
if ([buttonTitle isEqualToString:@"Cancel"]){
NSLog(@"User pressed the Cancel button.");
}
else if ([buttonTitle isEqualToString:@"Ok"]){
NSLog(@"User pressed the Ok button.");
}
//接受输入类容
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(@"%@",textField.text);
UITextField *textField2 = [alertView textFieldAtIndex:1];
NSLog(@"%@",textField2.text);
}
@end
下一篇: IOS 中的设置捆绑包