美文网首页
警告框和操作表

警告框和操作表

作者: 大荣纸 | 来源:发表于2017-04-15 11:28 被阅读18次

    使用步骤:
    step1:创建控制器实例,同时设定title+message,通过style来确定本次创建的是警告框还是操作表
    step2:创建所需的UIAlertAction实例,同时设定按钮上的title,并且在block中设定,点击按钮后要做的事情
    step3:将action添加到controller中
    step4:如果创建的是警告框,则可以添加textField,操作表绝对不能添加textField
    step5:使用当前控制器的presentViewController方法推出AlertController
    上代码:
    警告框:

    //点击空白处
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        //1.创建AlertController
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
        
        //2.创建界面上的按钮
        UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //获取在文本框中输入的用户名和密码
            NSString *name = alert.textFields[0].text;
            NSString *pwd = alert.textFields[1].text;
            NSLog(@"name=%@   pwd=%@",name,pwd);
        }];
        
        UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"NO");
        }];
        
        //3.将按钮添加到AlertController中
        [alert addAction:actionNo];
        [alert addAction:actionYes];
        
        //4.(可选)为AlertController添加文本框
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            //配置添加的用户名文本框
            textField.placeholder = @"用户名";
        }];
        
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            //配置添加的密码文本框
            textField.textColor = [UIColor redColor];
            textField.secureTextEntry = YES;
        }];
        
        //5.显示AlertController
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    

    操作表:

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"转发" style:UIAlertActionStyleDefault handler:nil];
        
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"回复" style:UIAlertActionStyleDefault handler:nil];
        
        UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
        
        [actionSheet addAction:action4];
        [actionSheet addAction:action2];
        [actionSheet addAction:action3];
        [actionSheet addAction:action1];
        
        [self presentViewController:actionSheet animated:YES completion:nil];
    }
    

    相关文章

      网友评论

          本文标题:警告框和操作表

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