UIAlertController的简单使用示例
来源:互联网 发布:js foreach return 编辑:程序博客网 时间:2024/06/05 06:29
UIAlertController的基本设置和一些简单的使用方法,代码如下
#import "ViewController.h"
@interface ViewController ()
{
UILabel * _lable;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray * array = @[@"普通",@"警示",@"登录",@"数目"];
for (int i = 0; i < array.count; i++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20 + i * 60 , 50, 50, 40);
button.tag = i + 100;
[button setTitle:array[i] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
_lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, 100, 100)];
_lable.backgroundColor = [UIColor redColor];
[self.view addSubview:_lable];
}
-(void)click:(UIButton *)button{
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction * defAction = [UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil];
switch (button.tag - 100) {
case 0:
{
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"通知" message:@"正常通知栏" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:cancelAction];
[alert addAction:defAction];
[self presentViewController:alert animated:YES completion:nil];
}
break;
case 1:
{
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"通知" message:@"警示通知栏" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:cancelAction];
UIAlertAction * warningAction = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:warningAction];
[self presentViewController:alert animated:YES completion:nil];
}
break;
case 2:
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登录";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *login = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
_lable.text = [login.text stringByAppendingString:login.text];
_lable.text = [login.text stringByAppendingString:password.text];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
break;
case 3:
{
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"通知" message:@"用户名不少于三个字符" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"用户名";
textField.secureTextEntry = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
_lable.text = alert.textFields.firstObject.text;
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
action.enabled = NO;
}
break;
default:
break;
}
}
- (void)alertTextFieldDidChange:(NSNotification *)notification{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = login.text.length > 2;
}
}
@end
- UIAlertController的简单使用示例
- UIAlertController的使用示例
- UIAlertController的简单使用
- Swift - UIAlertController的简单使用
- UIAlertController的用法示例
- UIAlertController和UIAlertAction的简单使用
- iOS9 升级后UIAlertController的简单使用
- UIAlertController简单使用
- iOS9 UIAlertController简单使用代码
- <swift学习之路> UIAlertController的简单使用
- UIAlertController的使用(Swift)
- UIAlertController的使用二
- UIAlertController的使用
- UIAlertController的使用
- 关于UIAlertController的使用
- UIAlertController的使用
- UIAlertController 的使用
- UIAlertController 的使用
- c++第一次实验-2
- keychain 专研
- 基于Hibernate的动态查询(HQL、Criteria、DetachedCriteria、MySQL)
- C++第一次实验-2
- UIWebView内存占用过高、崩溃、内存溢出
- UIAlertController的简单使用示例
- java学习第一集:Hello World
- UItableView处于UITableViewStylePlain模式下如何取消HeaderView粘连性
- Java中正则表达式匹配的语法规则
- Android eclipse导入项目后出现Unable to resolve target 'android-17'解决方法
- 平台提取his数据,自动填充到他表详解
- getResource("")和getClassLoader().getResource("")
- Python基础
- quick-cocos2d-x数据存储之GameState