UIAlertView和UIAlertController那点事儿

二、最基本的警告框

1、第一种方法

        UIAlertView * alter = [[UIAlertView alloc]initWithTitle:@"标题" message:@"这个是UIAlertView的默认样式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];

        [alter show];

2、第二种方法

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertView的默认样式" preferredStyle: UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:

                                       UIAlertActionStyleCancel handler:nil];

        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"登录"style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"删除,....");

        }];     

        [alertController addAction:cancelAction];

        [alertController addAction:deleteAction];

       //记得跳转

       [self presentViewController:alertController animated:YES completion:nil];





二、带有输入框的弹出框

        UIAlertController *alertController = [UIAlertController       alertControllerWithTitle:@"文本对话框" message:@"登录和密码的对话框示例" preferredStyle:UIAlertControllerStyleAlert];

        //UIAlertControllerStyleActionSheet

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            // 可以在这里对textfield进行定制,例如改变背景色

            textField.backgroundColor = [UIColor orangeColor];

        }];

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            // 可以在这里对textfield进行定制,例如改变背景色

            textField.backgroundColor = [UIColor orangeColor];

        }];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:

                                       UIAlertActionStyleCancel handler:nil];

        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            UITextField *login = alertController.textFields.firstObject;

            UITextField *password = alertController.textFields.lastObject;

            NSLog(@"帐号:%@",login.text);

            NSLog(@"密码:%@",password.text);

        }];     

        [alertController addAction:cancelAction];

        [alertController addAction:deleteAction];

       //记得跳转

       [self presentViewController:alertController animated:YES completion:nil];




三、带有多个按钮删除类型的弹出框

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或删除数据" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];

        //UIAlertControllerStyleActionSheet

        

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            // 可以在这里对textfield进行定制,例如改变背景色

            textField.backgroundColor = [UIColor orangeColor];

            //NSLog(@"ddd:%@",textField);

        }];

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            // 可以在这里对textfield进行定制,例如改变背景色

            textField.backgroundColor = [UIColor orangeColor];

        }];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:

                                       UIAlertActionStyleCancel handler:nil];

        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"quxiao,....");

//            UITextField *login = alertController.textFields.firstObject;

//            UITextField *password = alertController.textFields.lastObject;

//            NSLog(@"帐号:%@",login.text);

//            NSLog(@"密码:%@",password.text);

        }];

        UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:nil];

//

        [alertController addAction:cancelAction];

        [alertController addAction:deleteAction];

        [alertController addAction:archiveAction];

        

        [self presentViewController:alertController animated:YES completion:nil];




猜你喜欢

转载自blog.csdn.net/YJ909364/article/details/51982030
今日推荐