UIAlertController使用总结

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

1、创建UIAlertController,指定警告和操作样式

2、向其中添加按钮

3、显示UIAlertController

操作表样式:

- (void)createAlertController_sheet{


    /*

     先创建UIAlertController,preferredStyle:选择UIAlertControllerStyleActionSheet,这个就是相当于创建8.0版本之前的UIActionSheet;


     typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

     UIAlertControllerStyleActionSheet = 0,

     UIAlertControllerStyleAlert

     } NS_ENUM_AVAILABLE_IOS(8_0);


     */


    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"测试标题" message:@"测试内容..." preferredStyle:UIAlertControllerStyleActionSheet];


    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"block1回调");

    }];


    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"block2回调");

    }];


    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"block3回调");

    }];


    [actionSheetaddAction:action1];

    [actionSheetaddAction:action2];

    [actionSheetaddAction:action3];


    [self presentViewController:actionSheet animated:YES completion:nil];

}

警告样式:

- (void)createAlertController_alert{


    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题2" message:@"没有内容,随便写点东西" preferredStyle:UIAlertControllerStyleAlert];


    [alertaddTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder=@"输入账号";

    }];


    [alertaddTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder=@"输入密码";

    }];


    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"block1回调");

        //textFields是一个数组,获取所输入的字符串

        NSLog(@"%@",alert.textFields.lastObject.text);

    }];


    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"block2回调");

    }];


    [alertaddAction:action1];

    [alertaddAction:action2];


    [self presentViewController:alert animated:YES completion:nil];

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容