博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIAlertController样式集合
阅读量:4219 次
发布时间:2019-05-26

本文共 3900 字,大约阅读时间需要 13 分钟。

1.默认样式

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你好" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"点击了取消");    }];    UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"点击了确认");    }];    [alertController addAction:cancleAction];    [alertController addAction:commitAction];    [self presentViewController:alertController animated:YES completion:nil];

2.输入框样式

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登陆和密码对话框" preferredStyle:UIAlertControllerStyleAlert];    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.placeholder = @"登陆";        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];    }];    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {        textField.placeholder = @"密码";        /**         *  是否隐藏输入         */        textField.secureTextEntry = YES;        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];    }];        UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        /**         *  这里面可以做一些与服务器端交互的事情         */        NSLog(@"%@\n", alertController.textFields.firstObject.text);        NSLog(@"%@", alertController.textFields.lastObject.text);        [[NSNotificationCenter defaultCenter] removeObserver:self];    }];    commitAction.enabled = NO;    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"用户取消了登陆");        [[NSNotificationCenter defaultCenter] removeObserver:self];    }];    /**     *  添加交互按钮     */    [alertController addAction:cancleAction];    [alertController addAction:commitAction];        [self presentViewController:alertController animated:YES completion:nil];
/** *  监听textField是否改变 */- (void)alertTextFieldDidChange:(NSNotification *)nc{    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;    if (alertController) {        UITextField *userField = alertController.textFields.firstObject;        UITextField *passWordField = alertController.textFields.lastObject;        UIAlertAction *commitAction = alertController.actions.lastObject;        commitAction.enabled = userField.text.length > 2 && passWordField.text.length > 6;        NSLog(@"%d", commitAction.enabled);    }}
3.上拉菜单样式

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存和删除数据" message:@"删除数据不可恢复" preferredStyle:UIAlertControllerStyleActionSheet];    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"取消");    }];    UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"确定");    }];    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        NSLog(@"保存");    }];    [alertController addAction:cancleAction];    [alertController addAction:commitAction];    [alertController addAction:saveAction];    [self presentViewController:alertController animated:YES completion:nil];

你可能感兴趣的文章
利用db2look查看ddl
查看>>
java中的mmap实现
查看>>
Redis的Aof被阻塞原因调查
查看>>
Redis Cluster的FailOver失败案例分析
查看>>
Android Alarm驱动源代码分析(Alarm.c)
查看>>
S3C2440上LCD驱动 (FrameBuffer)实例开发讲解
查看>>
Linux音频编程指南
查看>>
usb-otg-调试心得
查看>>
USB规范浏览--设备和主机规范
查看>>
男人的品位--我们自己的最求
查看>>
Android (Linux) Suspend流程
查看>>
LINUX时间管理
查看>>
定时器的使用
查看>>
为Android加入busybox工具
查看>>
使用技巧busybox
查看>>
如何查看与/dev/input目录下的event对应的设备
查看>>
bootloader-bootable解析
查看>>
bootloader (LK)&&android lk bootloader中相关修改指南
查看>>
SD卡驱动分析--基于高通平台
查看>>
SD Card 驱动流程分析
查看>>