UIAlertController 的一般使用

2016-08-11  本文已影响19人  贵哥jk

UIAlertController 的一般使用

说明

展示效果

UIAlertController.png

思路

  1. 创建UIAlertController实例 A

代码实现

- (void)alertControllerTest{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"登录提示" message:@"请您输入账号,密码!" preferredStyle:UIAlertControllerStyleAlert];

   
    [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//        textField.tag = 1;
        textField.placeholder = @"请输入账号";
    }];
    [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//        textField.tag = 2;
        textField.placeholder = @"请输入密码";
    }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//        UITextField *textField1;
//        UITextField *textField2;
//        
//        for (UITextField *textField in alertC.textFields) {
//            if (textField.tag == 1) {
//                textField1 = textField;
//                continue;
//            }else if (textField.tag == 2){
//                textField2 = textField;
//                continue;
//            }
//        }
//        
//        
//        NSLog(@"点击了确定按钮:%@\n%@",textField1.text,textField2.text);
        
               
        NSString *text1 = alertC.textFields[0].text;
        NSString *text2 = alertC.textFields[1].text;
        NSLog(@"%@\n%@",text1,text2);
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
    [alertC addAction:action1];
    [alertC addAction:action2];

    
    [self presentViewController:alertC animated:YES completion:nil];
    
    
}
上一篇 下一篇

猜你喜欢

热点阅读