美文网首页
UIAlertController的三种使用

UIAlertController的三种使用

作者: 原来可以这样 | 来源:发表于2018-04-02 10:21 被阅读85次

其他不多说,代码是王道


第一种平常看到可以打电话的那种模式



//preferredStyle: 设置弹窗类型

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"天朝帝都" message:@"我是客服" preferredStyle:(UIAlertControllerStyleActionSheet)];

//字体颜色

alertView.view.tintColor = [UIColor redColor];

//style:设置提示弹窗样式

UIAlertAction *select = [UIAlertAction actionWithTitle:@"给我打个电话聊聊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"你打电话了");

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"你选择了取消");

}];

UIAlertAction *other = [UIAlertAction actionWithTitle:@"这是什么情况" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"这是什么情况");

}];

[alertView addAction:select];

[alertView addAction:cancel];

[alertView addAction:other];

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




第二种有输入框的那种模式


需要注意的是,这里只能使用老版的弹窗提示模式,不能使用脚底弹窗

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"就提示一下吧" preferredStyle:(UIAlertControllerStyleAlert)];

[alertView addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.keyboardType = UIKeyboardTypeNumberPad;

textField.placeholder = @"请输入用户名";

}];

[alertView addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.keyboardType = UIKeyboardTypeNumberPad;

textField.placeholder = @"请输入密码";

}];

UIAlertAction *alertText = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

//获取第一个输入框

UITextField *textFieldUser = [[alertView textFields] firstObject];

//获取第二个输入框

UITextField *textFieldPassWord = [[alertView textFields] objectAtIndex:1];

if (textFieldUser.text.length == 0) {

//这里加入自己的逻辑

NSLog(@"用户名");

}else if (textFieldPassWord.text.length == 0){

NSLog(@"密码不为空");

}

}];

UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"点击取消");

}];

[alertView addAction:cancleAction];

[alertView addAction:alertText];

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



第三种弹窗提示模式

UIAlertController *alertView  = [UIAlertController alertControllerWithTitle:@"提示" message:@"普通的提示信息" preferredStyle:(UIAlertControllerStyleActionSheet)];

UIAlertAction *showAlert = [UIAlertAction actionWithTitle:@"提示按钮一" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"你点击了提示按钮");

}];

UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"提示按钮二" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"你点击了提示按钮二");

}];

[alertView addAction:showAlert];

[alertView addAction:cancelAlert];

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


相关文章

网友评论

      本文标题:UIAlertController的三种使用

      本文链接:https://www.haomeiwen.com/subject/simqhttx.html