创建IOS的alert界面

//
//  MyViewController.m
//  Demo6_Alert警告框
//
//  
//

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (IBAction)buttonClick:(id)sender {
    //需要弹出警告框
    //1.创建UIAlertController实例
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    //2.创建意图按键
    UIAlertAction *actionYES = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //把AlertController中所有的 textField取出
        NSArray *fields = alert.textFields;
        //把 第一个添加的 textField取出
        UITextField *userNameField = fields[0];
        //把 第二个添加的 textField取出
        UITextField *pwdField = fields[1];
        //判断用户名是否是 abc  密码 是否是 123
        if ([userNameField.text isEqualToString:@"abc"] && [pwdField.text isEqualToString:@"123"]) {
            //点钟action后做什么事情
            NSLog(@"登录成功");
            //重新弹出一个对话框, title 提示  message 登录成功  action 确定
        }else {
            NSLog(@"登录失败");
            
        }
    }];
    
    
    
    UIAlertAction *actionNO = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"actionNO 被点中");
    }];
    //3.将 alertAction 添加到 AlertController中
    [alert addAction:actionYES];
    [alert addAction:actionNO];
    
    //4.向AlertController中添加textField
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //在bolck中可以设置 今天添加的textField进行设置
        //设置 占位符
        textField.placeholder = @"请重新输入用户名";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.textColor = [UIColor redColor];
         textField.placeholder = @"请重新输入密码";
        textField.secureTextEntry = YES;//密码样式
    }];
    
    
    
    
    
    //5.显示AlertController
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


你可能感兴趣的:(创建IOS的alert界面)