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

警告框和操作表

作者: 大荣纸 | 来源:发表于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];
}

相关文章

  • 警告框和操作表

    使用步骤:step1:创建控制器实例,同时设定title+message,通过style来确定本次创建的是警告框还...

  • iOS学习之UIAlertController警告和操作表

    应用是如何和用户进行交流的呢?警告框和操作表就是为此而设计的。 iOS警告框用于给用户提示和警告,最多有2个按钮,...

  • swift中UIActionSheet的使用

    Swift - 操作表(UIActionSheet)的用法,也叫底部警告框: 1,下面创建一个操作表(或叫底部警告...

  • [iOS学习]警告框和操作表

    警告框 iOS中警告框给用户警告或提示,最多两个按钮,超过两个就应该使用操作表。由于在iOS中警告框是模态的,因此...

  • UIAlertController使用总结

    UIAlertController有两种形式,警告框和操作表,创建步骤都差不多。 1、创建UIAlertContr...

  • UIAlertController使用——展示AlertView

    在iOS8之前使用UIAlertView和UIActionSheet来展示警告框和操作表,自iOS8之后统一为通过...

  • 警告框处理

    弹出警告框,我们用switch_to.alert方法定位到警告框,然后再执行我们想要的操作 获取警告框文本text...

  • UIAlertController使用

    当你的应用程序需要向用户呈现重要信息,或提示用户重要选择时,可以使用警告框(Alert View)和操作表(Act...

  • UIAlertController的使用

    当你的应用程序需要向用户呈现重要信息,或提示用户重要选择时,可以使用警告框(Alert View)和操作表(Act...

  • Swift - 操作表(UIActionSheet)的用法,也叫

    1,下面创建一个操作表(或叫底部警告框)并弹出显示 (注:自IOS8起,建议使用UIAlertController...

网友评论

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

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