UIAlertContronller的使用

来源:互联网 发布:实战nginx pdf 编辑:程序博客网 时间:2024/05/29 11:06

苹果公司已经建议开发使用UIAlertController来替代UIActionSheet和UIAlertView,把这两个合并到了一个类里来实现他们,所以我们跟上潮流!下面总结一下!
老规矩,直接上代码,不废话!

这段代码的背景是选取系统的照片
这里写图片描述

- (void)setActionSheet{    //初始化UIAlertController    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];    /*    UIAlertControllerStyle有两种,一种是UIAlertControllerStyleActionSheet(相当于UIActionSheet),另一种是UIAlertControllerStyleAlert(相当于UIAlertView)    */    //确定了UIAlertController的style,我们要添加action 了(button或者textfield)    [alertController addAction:[UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        //处理拍照    }]];    /*    在添加UIAlertAction时,要选它的style!    UIAlertAction的style有以下3种:    UIAlertActionStyleDefault = 0,//默认    UIAlertActionStyleCancel,//取消    UIAlertActionStyleDestructive //有可能改变或者数据    */    [alertController addAction:[UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        //处理相册    }]];    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];    [self presentViewController:alertController animated:YES completion:nil];}

到这里,一个“UIActionSheet”就可以运行了!

下面说下“UIAlertView”!
这里写图片描述

- (void)setAlertView{    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录" message:@"请输入帐户密码" preferredStyle:UIAlertControllerStyleAlert];//注意这里的UIAlertController的style是UIAlertControllerStyleAlert,而不是UIAlertControllerStyleActionSheet    //Textfield的添加  注意:只能是 UIAlertControllerStyleAlert才能添加Textfield!    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.placeholder = @"please input your name";        textField.textColor = [UIColor blackColor];        textField.clearButtonMode = UITextFieldViewModeWhileEditing;        textField.borderStyle = UITextBorderStyleRoundedRect;    }];    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        textField.placeholder = @"password";        textField.textColor = [UIColor blueColor];        textField.clearButtonMode = UITextFieldViewModeWhileEditing;        textField.borderStyle = UITextBorderStyleRoundedRect;        textField.secureTextEntry = YES;    }];    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSArray * textfields = alertController.textFields;        UITextField * namefield = textfields[0];        UITextField * passwordfiled = textfields[1];        NSLog(@"%@:%@",namefield.text,passwordfiled.text);    }]];    [self presentViewController:alertController animated:YES completion:nil];}

说下另一种情况的“UIAlertView”!
这里写图片描述
代码写在了懒加载里

- (UIAlertController *)alertController{    if (!_alertController) {        _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您是否确认提交?" preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil];        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            //在这里写确认后你要干的事.        }];        [_alertController addAction:cancelAction];        [_alertController addAction:okAction];    }    return _alertController;}

到这里你应该掌握了吧!
参考了大神的资料,如果有不对之处,希望批评指正!
我是菜鸟,但我有一颗大神的心!

0 0
原创粉丝点击