创建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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值