OC 提示框 UIAlertController(UIAlertView)


实现环境:XCode7.2.1 IOS 9.2


一、UIAlertController 简介

UIAlertController 是IOS8中替代UIAlert及UIActionSheet而产生的一个类,
UIAlertController 可以产生 对话框, 提示框,上拉式菜单等功能


二、UIAlertController 用法

2.1 创建一个简单的对话框

    #pragma mark 2.1
    UIAlertController *alertTest =
    [UIAlertController alertControllerWithTitle:@"测试弹出框标题"
                       message:@"这是一个测试消息"
                       preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 =
                        [UIAlertAction actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault  
          //设置按钮风格  UIAlertActionStyleDefault默认 UIAlertActionStyleDestructive 按钮被设置为红色
                         handler:nil]; //添加一个按钮,事件设置为nil

    UIAlertAction *action2 =
                        [UIAlertAction actionWithTitle:@"cancel"
                         style:UIAlertActionStyleDefault
                        handler:nil]; //添加一个按钮 事件设置为nil
    
    
    [alertTest addAction:action1]; //将按钮添加到UIAlertController
    [alertTest addAction:action2];//将按钮添加到UIAlertController
    
    [self presentViewController:alertTest
                       animated:YES completion:nil]; //将UIAlertController 再当前对象打开

UIAlertController弹出窗效果

UIAlertController弹出窗效果




2.2 UIAlertController 样式修改

下面的例子中,我们将向UIAlertController中添加”文本输入框”,例:我们常见的 弹出框中,接收文本的输入

- (IBAction)btnClose:(id)sender {

    #pragma mark 2.2 生成自定义alertControll
    UIAlertController *alertTest =
    [UIAlertController alertControllerWithTitle:@"登录"
                        message:@"请输入用户名和密码" preferredStyle:UIAlertControllerStyleAlert];
    //生成一个alertController
    
    [alertTest addTextFieldWithConfigurationHandler:^(UITextField *txtField){
        txtField.placeholder =@"用户名";
    }];
    //插入一个textField 并加入默认字符"用户名"
    
    [alertTest addTextFieldWithConfigurationHandler:^(UITextField *txtField) {
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(txtChange:)
            name:UITextFieldTextDidChangeNotification object:txtField];
        txtField.placeholder =@"密码";
        txtField.secureTextEntry = YES;
    }];
    
    //插入一个textField 并添加监控事件"txtChange"
    
    
    
    UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"登录"
        style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *login = alertTest.textFields.firstObject;
        NSLog(@"用户名.密码:");
        NSLog(login.text);
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"ok"
        style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        [[NSNotificationCenter defaultCenter] removeObserver:self
          name:UITextFieldTextDidChangeNotification object:nil];
        //移除事件
    }];
    
    
    action2.enabled =NO;
    
    [alertTest addAction:loginAction];
    [alertTest  addAction:action2];
    
    [self presentViewController:alertTest
                       animated:YES completion:nil]; //将UIAlertController 再当前对象打开

    
    
    
}


-(void)txtChange:(NSNotification *)notification{
    UIAlertController *alertTest = (UIAlertController *)self.presentedViewController;
    NSLog(@"tetst");
    if(alertTest){
        NSLog(@"监控开始!");
        UITextField *t  = alertTest.textFields.lastObject;
        UIAlertAction *action2 = alertTest.actions.lastObject;
        action2.enabled = t.text.length >=3;
    }
}

UIAlertController样例-输入框


2.3 UIAlertController 下拉式按钮


- (IBAction)btnClose:(id)sender {

    #pragma mark 2.3 OC生成弹出式提示框alertControll
    
    UIAlertController *alertTest = [UIAlertController
      alertControllerWithTitle:@"弹出式提示框"
      message:@"提示信息"
      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 =
    [UIAlertAction actionWithTitle:@"ok"
                             style:UIAlertActionStyleDestructive
                           handler:nil]; //添加一个按钮 事件设置为nil
    
    UIAlertAction *action2 =
    [UIAlertAction actionWithTitle:@"cancel"
                             style:UIAlertActionStyleDefault
                           handler:nil]; //添加一个按钮 事件设
    
    
    [alertTest addAction:action1];
    [alertTest addAction:action2];
    
    
    UIPopoverPresentationController *popOver = [alertTest  popoverPresentationController];
    popOver.sourceView = _btnCloseOB; //下拉式对话框 弹出的位置
    popOver.sourceRect = _btnCloseOB.bounds;
    
    [self presentViewController:alertTest animated:YES completion:nil];
     
}


常见异常:Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller’s popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.

以上的异常,说明没有指定下拉式提示框的位置:
需加入以下代码 :
UIPopoverPresentationController *popOver = [alertTest popoverPresentationController];
popOver.sourceView = _btnCloseOB; //下拉式对话框 弹出的位置
popOver.sourceRect = _btnCloseOB.bounds;
运行效果:
UIAlertController样例-下拉式