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


0 0
原创粉丝点击