UI - UIAlertViewAndUIAlerController

来源:互联网 发布:怎么查看手机支持网络 编辑:程序博客网 时间:2024/06/11 01:40

UIAlertView 官方文档介绍: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 preferredStyleof UIAlertControllerStyleAlert.

虽然UIAlerView 被弃用,但是目前还可以使用

- (void)configureAlertViewOnTheView{    // iOS 8 later is deprecated    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题1" message:@"内容1" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];    [alertView addButtonWithTitle:@"都不选"];    /**     * 风格类型     UIAlertViewStyleDefault = 0,     UIAlertViewStyleSecureTextInput,  //有一个不加密的textfield     UIAlertViewStylePlainTextInput,   //有一个textfield加密框     UIAlertViewStyleLoginAndPasswordInput   //有两个textfield,placeHolder 是 Login和password     */    alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;    [alertView show];    //判断是否显示,只读    if (alertView.visible) {        NSLog(@"当前提示正在显示");    }    //通过代码完成点击buttonIndex的button 并dismiss 当前alertView的操作//    [alertView dismissWithClickedButtonIndex:0 animated:YES];        alertView.delegate = self;}//点击按钮时出发的方法-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSString *title = alertView.title;    NSString *message = alertView.message;    NSString *bttonTitle = [alertView buttonTitleAtIndex:buttonIndex];    NSInteger num = [alertView numberOfButtons];    NSInteger cancelIndex = [alertView cancelButtonIndex];    UITextField *textF = [alertView textFieldAtIndex:0];    NSLog(@"title=%@,message=%@,butonTitle=%@,按钮数%ld,取消按钮位置%ld,获取 alertView 上的 textfield=%@",title,message,bttonTitle,num,cancelIndex,textF);    }//设置alertView 的第一个otherButton的enable属性;The alert view that is being configured.-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{    return NO;}-(void)willPresentAlertView:(UIAlertView *)alertView{    NSLog(@"即将出现");}-(void)didPresentAlertView:(UIAlertView *)alertView{    NSLog(@"已经出现");}-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{    NSLog(@"即将消失");}-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{    NSLog(@"已经消失");}-(void)alertViewCancel:(UIAlertView *)alertView{    NSLog(@"点击了取消按钮");}


 UIAlerController介绍

UIAlertController 同时替代了 UIAlertView 和 UIActionSheet,从系统层级上统一了 alert 的概念。UIAlertController 不管是要用 alert 还是 action sheet 方式展示,都要以 title 和 message 参数来初始化。Alert 会在当前显示的 view controller 中心以模态形式出现,action sheet 则会在底部滑出。Alert 可以同时有按钮和输入框,action sheet 仅支持按钮。

新的方式并没有把所有的 alert 按钮配置都放在初始化函数中,而是引入了一个新类 UIAlertAction 的对象(及addTextFieldWithConfigurationHandler: 方法),在初始化之后可以进行配置。这种形式的 API 重构让对按钮数量、类型、顺序方便有了更大的控制。同时也弃用了 UIAlertView 和 UIActionSheet 使用的delegate 这种方式,而是采用更简便的完成时回调。

- (void)configureUIAlertControllerOnTheView{    /**     *  风格类型,     UIAlertControllerStyleActionSheet = 0, //     UIAlertControllerStyleAlert     */    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题2" message:@"内容2" preferredStyle:UIAlertControllerStyleAlert];        /**     * 风格类型,     UIAlertActionStyleDefault = 0,     UIAlertActionStyleCancel,     UIAlertActionStyleDestructive     */    UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"确定");        NSString *title = alertVC.title;        NSString *message = alertVC.message;        UIAlertAction *act = alertVC.preferredAction;        //        UIAlertControllerStyle style = alertVC.preferredStyle;        //action 数组        NSArray *arr_action = alertVC.actions;        //textfield 数组        NSArray *arr_tf = alertVC.textFields;                NSLog(@"title=%@,message=%@,首选的 action=%@,当前的风格=%ld,action数组=%@,textfield 数组=%@",title,message,act,style,arr_action,arr_tf);    }];    UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"不确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"不确定");    }];    UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"取消");    }];    [alertVC addAction:action_1];    [alertVC addAction:action_2];    [alertVC addAction:action_3];        //当 alerVC 为 UIAlertControllerStyleAlert时,才可以添加textfield    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.backgroundColor = [UIColor lightGrayColor];        textField.placeholder = @"请输入第一个";        textField.secureTextEntry = NO;    }];    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.backgroundColor = [UIColor  whiteColor];        textField.placeholder = @"请输入第二个";        textField.secureTextEntry = YES;    }];        [self presentViewController:alertVC animated:YES completion:nil];}


0 0