UIAlertController的使用,代替UIAlertView和UIActionSheet

在iOS8以后,UIAlertView就开始被抛弃了。

取而代之是UIAlertController

以前是警示框这样写:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入用户名、密码和服务器" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [alert show];

效果如图:

现在是这样写:

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名、密码和服务器" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消按钮");
        }];
        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了确定按钮");
        }];

        [alertController addAction:cancelAction];
        [alertController addAction:OKAction];

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

效果如图:

添加多个Action会自动向下排列

上面代码中再添加一个

UIAlertAction *DesAction = [UIAlertAction actionWithTitle:@"Destory" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击了Destory按钮");

        }];

[alertController addAction:DesAction];

如果提示菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部

//冻结确定按钮
        OKAction.enabled = NO;

添加文本框

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"Email";
            textField.keyboardType = UIKeyboardTypeEmailAddress;
        }];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"Password";
            textField.secureTextEntry = YES;
        }];

UIActionSheet也用这个取代了。。方法是在初始化的时候把类型改一下就OK。但是UIActionSheet是不能加文本框的!!

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名、密码和服务器" preferredStyle:UIAlertControllerStyleActionSheet];

P.S 用通知监听用户名和密码输入, 这里有篇介绍得很好的文章:

http://blog.csdn.net/lengshengren/article/details/39896037

时间: 2024-08-12 01:14:38

UIAlertController的使用,代替UIAlertView和UIActionSheet的相关文章

iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. //UIAlertView和UI

UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案

本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertController代替: 1.Alert用法 UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"This is Title" message:@"This is message" prefer

iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的漂亮人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 弃用了! 如今得用 UIAlertController 了. 使用两个样式来相应这两个控件: UIAlertView - UIAlertContr

IOS UIAlertView 和 UIActionSheet的区别

UIAlertView 和 UIActionSheet的区别: 1.弹框位置不同: UIAlertView弹框显示在中间 UIActionSheet弹框显示在底端 2.是否可以实现文本框的输入(参考:http://www.ithao123.cn/content-9409772.html) UIAlertView可以实现,而UIActionSheet不可以实现. #import "RootViewController.h" @interface RootViewController ()

在iOS8.0之后的UIAlertView和UIActionSheet的新写法

在iOS8.0之后,苹果更新了UIAlertView和UIActionSheet的创建方法.在以前的版本中,这两个提醒用户的控件各自有自己的创建方法,但是在iOS8.0之后,使用了UIAlertController这个控制器类统一创建.不过之前的创建方法并没有在iOS8.0之后的版本中实效,依然可以正常使用.下边就记录下新的写法. 首先看苹果API的示例写法: UIAlertController* alert = [UIAlertController alertControllerWithTit

UIAlertView、UIActionSheet兼容iOS8

1.前言 iOS8新增了UIAlertController来代替UIAlertView.UIActionSheet的使用.本文在不使用UIAlertController的情况下,用最简单的方法让UIAlertView.UIActionSheet兼容iOS8. 2.UIAlertView iOS8下,如果UIAlertView初始化的时候title为nil的话,iOS8下面弹框中message会变成粗体,并且过于靠近顶部,为了保存跟iOS8之前的版本一致,只需要在初始化的时候将title置为@"&

UIAlertView和UIActionSheet

##UIAlertView和UIActionSheet ```objc// cancelButton buttonIndex == 1// destructiveButton buttonIndex ==0 UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"确定注销吗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@&q

iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet

UIAlertView/UIActionSheet UIAlertView 1 //一个按钮的提醒 2 @IBAction func oneButtonAler() 3 { 4 //创建单一按钮提醒视图 5 let oneButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定")

IOS学习第二课 UIAlertView和UIActionSheet

1    UIAlertView 类似于Android中的Dialog,简单用法如下: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Messate" delegate:nil cancelButtonTitle:@"Cancle" otherButtonTitles:nil, nil]; [alertView show]; 2   U